-2
cout << "What name would you like fo you output file?\n";
getline(cin, file);

outfile.open(file.c_str());   

This works fine but I need save my file be save as .txt format

  • 1
    Append `.txt` to what they type. Although with that said the extension never guarantees content. – drescherjm Sep 26 '18 at 18:21
  • Do `file += ".txt";` – 138 Sep 26 '18 at 18:24
  • i have tried to add that outfile.open(file.c_str().txt) – Rosario Monroy Sep 26 '18 at 18:25
  • 1
    What is your question? Are you asking what to do when the file name does not end in `.txt`? Are you asking how to check if it ends in `.txt`? Are you asking how to append `.txt` to the end of the file name? – Drew Dormann Sep 26 '18 at 18:25
  • 1
    ***i have tried to add that outfile.open(file.c_str().txt)*** That is not the correct way of appending to a string. Append to the string before you try to open. @138 showed you how. – drescherjm Sep 26 '18 at 18:25
  • @RosarioMonroy `file.c_str()` returns a const char * type. Which is not a struct/class so you cannot use dot operator on it. – 138 Sep 26 '18 at 18:26
  • @RosarioMonroy please don't change the question after it is answered to a totally different topic. Ask a completely new question instead. Remember that StackOverflow is not a forum. The purpose of your question is to help future readers with the same problem. – drescherjm Sep 26 '18 at 20:18
  • Also when you do ask a new question please be more detailed with it than your attempted edit. You will probably have to show some code for that new question. – drescherjm Sep 26 '18 at 20:24
  • Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Rosario Monroy Oct 10 '18 at 22:14

1 Answers1

2
string ext = ".txt"; //file extension

cout << "What name would you like for your output file?\n";
getline(cin, file);
//highly recommend checking user input (for extensions, invalid symbols etc..)
file += ext; //appends ext to file  

outfile.open(file.c_str());

see http://www.cplusplus.com/reference/string/string/ for other operations allowed on std::string

138
  • 554
  • 5
  • 14
  • One negative to this approach is if the user typed an extension now it will have two however for this example its probably good enough (and the reason why I suggested it in the first place). – drescherjm Sep 26 '18 at 18:31
  • I agree. I'm assuming this is for a intro c++ class/project. A real project should *ALWAYS* verify any user input. – 138 Sep 26 '18 at 18:33
  • 1
    That will do it. Thank you. – Rosario Monroy Sep 26 '18 at 18:36
  • 1
    @RosarioMonroy There are more sophisticated solutions for that problem, e.g. instead of blindly appending `.txt` as the filename extension, check if it wasn't like that from the user input already. With the current C++ standard you can simply use a `std::string` as a parameter for `std::fstream::open()` function BTW, the usage of `.c_str()` is obsolete. – πάντα ῥεῖ Sep 26 '18 at 18:39