Data file

i need the variable recno to store the last value in the file called rfile. but when i run the following code, the other recno does not get stored in the file. please help out.
Thanks in advance :D

1
2
3
4
5
6
7
8
for(int i=0; !rfile.eof(); i++)
{   rfile>>recno;      
    std::cout<<recno;
}
:
:
:
rfile<<recno;   //storing the new recno 


i tried the following and only 23 got stored into the file...
1
2
3
4
5
6
7
for(int i=0; !rfile.eof(); i++)
        {   rfile<<"23";
            rfile>>recno;
            rfile<<"76";
            std::cout<<recno;
            rfile<<"10";
        }
Last edited on
That's because you only store the last one (line 8 of your code.)

You will need to save each value you read into a vector and then write out the entire vector at the end.

Or, write each value to a new file as you read the original file, then rename the new file to the old file's name.
i'm sorry, but i didn't get it..
You can't write to the same file you are reading. If you are going to write to a file instead of to a vector, you will have to write to a new file, then rename the new file to the old file.
oh thanks :D i got it. :)
thank you very much :D
Topic archived. No new replies allowed.