Jump to content
  • Advertisement
Sign in to follow this  
wayneprim

entering a string of data to an ofstream file

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a program that allows the user to do 2 things (create a new file in the current directory, or search a file and display the word count) The word count works fine but when I try to allow the user to create a file it starts going wrong. I first ask the user to input a filename, and then enter some initial data into a string variable. That string variable gets passed to the createFile() function which takes care of entering the data into the file.

When I run this program I enter the file data I wish to put in the created file, say for example " Hello File Data" and when I look into that file once its been created there is only the first word ("Hello") in the case of this example. Here is the code:

The main function:

[source lang="cpp"]

cout << "Enter the file name: " << endl;
cin >> filename;
outFile.open(filename.c_str());//create the file

if (!outFile)
{
//file could not be created
cout<< "File could not be completed" << endl;
break;

}
else // file was created
cout << "FILE WAS COMPLETED NAMED: " << filename << endl;;

//ask the user to put some data y/n
cout << "Would you like to set some initial data in the file? y/n" << endl;
cin >> choice;
switch(choice)
{
case 'y':
cout << "Enter the data: " << endl;

cin >> filedata;
user1.createFile(outFile, filedata);
break;
case 'n':
cout << "Ok then dont create initial data... Your created file is blank" << endl;
break;
}



[/source]

The create filefunction:
[source lang="cpp"]
void User::createFile(ofstream &outfile, string filedata)
{
//output the file into outfile
outfile << filedata;

cout << "You have successfully saved the data you entered" << endl;


}
[/source]

Please help! smile.png

Thanks

I will attach the zip folder as well just in case...

Share this post


Link to post
Share on other sites
Advertisement
Beside your zip packing (unnamed .zip in another zip) the problem is that you're using the input stream >> operator.
The input stream stops at the first blank it encounters.

Try std::getline:

std::getline ( std::cin, filedata );

Share this post


Link to post
Share on other sites
because when I use it in the main function, getline(cin, filedata), and then run the program i automatically skips that and doesn't let me enter anything.

Share this post


Link to post
Share on other sites
Thanks, but now when I change cin>> to getline() the program doesn't let me enter the data. Instead it just skips the data and jumps to your data has been entered successfully... Is there anyway to make the program "pause" to allow me to enter the data?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!