Jump to content
  • Advertisement
Sign in to follow this  
logank9

Using fstream to read/write certain lines from a file?

This topic is 3414 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

Does anyone know to read and write specific lines of files in fstream? Like if I had the file 3 4 1 How would I return the value of the third line? Or if I had the file 341 How would I get the third letter in the file? For writing the file, if I had 1 41 2 How would I set line 2 to 86? It would be cool if someone could help me on this. :D

Share this post


Link to post
Share on other sites
Advertisement
Reading the Nth line is easy. Using std::string and the free function std::getline(), simply loop from (i = 0 ; i < N ; ++i) and then return that string. With a little more code you can do error checking to ensure there are at least that many lines in the file.

Changing the Nth line is harder. The best way would be to read the file in its totality (perhaps into a container such as std::vector<std::string>, again using std::getline()). Then, change the Nth element in the vector to whatever you want, use a new ofstream object to write out the altered vector.

It is also quite possible to do this by simply counting the number of newlines too, if you want.

Share this post


Link to post
Share on other sites
Thank you very much! I'm still kind of confused on the change line part. Could I maybe rewrite the file?

Could I read all the lines up to that one and copy them onto the new file, put the new line in, and read all the lines after that and copy them into the new file? Or would this just be a waste?

Share this post


Link to post
Share on other sites
Quote:
Original post by logank9
Thank you very much! I'm still kind of confused on the change line part. Could I maybe rewrite the file?

Could I read all the lines up to that one and copy them onto the new file, put the new line in, and read all the lines after that and copy them into the new file? Or would this just be a waste?

That is exactly what he said =P Also what many programs do, because you can't shift bytes in a file - you need to rewrite them. In most cases, it's easier to just rewrite it all.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sik_the_hedgehog
Quote:
Original post by logank9
Thank you very much! I'm still kind of confused on the change line part. Could I maybe rewrite the file?

Could I read all the lines up to that one and copy them onto the new file, put the new line in, and read all the lines after that and copy them into the new file? Or would this just be a waste?

That is exactly what he said =P Also what many programs do, because you can't shift bytes in a file - you need to rewrite them. In most cases, it's easier to just rewrite it all.


Oh.... *facepalm*

Heh, I'm not very good with programming yet, I don't understand half this stuff.

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!