Opening Not Reading a txt file?
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?
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.
Tried this...
and this...
as said, neither work. What am I doing wrong?
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?
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.
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.
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement