• 12
• 11
• 9
• 10
• 13

# need help with file I/O

This topic is 4961 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am going to make a memo program where a user enters some text and it then outputs that text onto the desktop as a sort of memo. I have two questions. first, I need it so whatever the user types is stored in a variable(of course) and it then outputs the text to a file. however, I dont know how to do this. I know how to do this:
string smemo = "this is a memo";


however, I dont know how to make it where instead of printing "this is a memo", it prints what is in the variable. would I do something like this:
string smemo = memo;


memo would be the name of the variable. second question. how would I make it where the user can name the file himself? thanks!

##### Share on other sites
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){      //Variables and Initialization.      string memo = "";      ofstream fout;                 //Prompt user to enter a string and store it.      cout << "Enter String: ";      cin >> memo;      //This opens a file for output.      fout.open("Memo.txt");           //Checks to see if file open was successfull.      //If file opens write memo to the Memo.txt      if(fout.fail())      {            cout << "Error with file." << endl;           exit(1);      }      else      {           fout << memo;      }      //THis closes the file, since we don't need it anymore.      fout.close();        return(0);}

Here is the code for a little file output. If anyone sees any problems with this please fix it for us! =)
Here is a great tutorial that will help you a bit more, check it out.
Writing to a file.
http://www.gametutorials.com/Tutorials/c++/Cpp_Pg3.htm

Goodluck.

##### Share on other sites
ok, 1 discreet problem I spotted with that code is using std::cin will only input the first word before the first space. use getline() as shown below to fix that.

to let the user name the file himself, you would pass the name of what the user wants + ".txt" to open(). also, since you're using strings, you'll need to convert the string you input the name to to a c-style string using c_str(). something like this (this is just a modified version of VerKiViouS's code):

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){      //Variables and Initialization.      string memo = "";      string name = "";      ofstream fout;                 //Prompt user to enter a string and store it.      cout << "Enter String: ";      getline(std::cin, memo); //(new getline() function)      //Prompt user for file name      cout << "Enter name of memo: ";      getline(cin, name);      name += ".txt"; //Putting ".txt" on end      //This opens a file for output.      fout.open(name.c_str());           //Checks to see if file open was successfull.      //If file opens write memo to the Memo.txt      if(fout.fail())      {            cout << "Error with file." << endl;           exit(1);      }      else      {           fout << memo;      }      //THis closes the file, since we don't need it anymore.      fout.close();        return(0);}

EDIT: I unnecessarily added std:: to cin out of habit...fixed that