Using fstream to read/write certain lines from a file?
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
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.
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.
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?
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?
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.
Quote:Original post by Sik_the_hedgehogQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement