Jump to content
  • Advertisement
Sign in to follow this  
frogtag

Opening Not Reading a txt file?

This topic is 3991 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 know how to read and write to a text file using C++, but is there anyway to incorporate within my program a way of running/opening an existing text file?

Share this post


Link to post
Share on other sites
Advertisement
Pretty much exactly the same way. Specify the filename as you would normally, giving the name of an existing file. Then... just don't write to it.

Share this post


Link to post
Share on other sites
Tried this...

std::ofstream myfile;

myfile.open ("example.txt");
myfile << "Start...\n";
myfile << "...Finish\n";
myfile.close();

myfile.open ("example.txt");
myfile.close();


and this...

std::ofstream myfile;

myfile.open ("example.txt");
myfile << "Start...\n";
myfile << "...Finish\n";
myfile.close();

myfile.open ("example.txt");
myfile.close();


as said, neither work. What am I doing wrong?

Share this post


Link to post
Share on other sites
When you open a file like myfile.open("example.txt") it uses the default mode parameter myfile.open("example.txt", std::ios::trunc) which deletes what is in the file.

So when you call myfile.open("example.txt") the second time it will empty what is in the file and after that you don't write to it anymore and close it.

Share this post


Link to post
Share on other sites
Quote:
Original post by frogtag
Tried this...


std::ofstream myfile;

myfile.open ("example.txt");


as said, neither work. What am I doing wrong?


That means "open the file and write to it". std::ofstream is for output.

Also, the two things you tried appear to be identitcal. Sure you copied and pasted that correctly?

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!