Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How can i rewrite the first line of a line with out making a copy of file and rewriting it. I am using sequential writing for example my file contains data

hey<br />
how are you everyone at codeproject??


and i want to rewrite it as

hello!!<br />
how are you everyone at codeproject??


i tried this code
ofstream fout2("res.dat",ios::app);
	fout2.seekp(0, ios::beg);
	fout2<<str;
	fout2.close();

but it write str to the end of the file :(

Any suggestions ?
Posted

You cannot do that.
You have to copy file content to a memory buffer and then rewrite the file the way you depicted.
:-)
 
Share this answer
 
Comments
Nish Nishant 21-Apr-11 14:32pm    
Voted 5.
If your first line is always of a fixed size, you can do that. Else, you have to rewrite a new file with the modified data.
 
Share this answer
 
Some ideas discussed here.
 
Share this answer
 
Another approach is to append at he end of the file, and let a viewer reverse the sequence when read.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900