On 23 Jul 2004 18:01:04 -0700, Mahesha <yc******@hotmail.com> wrote:
Hello,
I'm new to C++ and I have requirement to open a existing text file in
write mode and write 2 new lines of text in the beginning of the file.
I'm working with fstream standard library.
If someone could direct me to a sample code to do this that would be
really helpful.
Thanks,
Mahesha
That cannot be done in any file system that I know of. With files you can
write to the end of a file, or you can write over the existing contents of
a file, but you cannot insert data into the beginning or middle of a file.
I would try an redesign your program so that you don't have to do this.
You are trying to use files in a way that they were not designed to be
used.
If you really cannot avoid this, then the only way to do what you want is
to write your two lines to a new file, and then copy the rest of the old
file to the end of the new file. This is obviously very inefficient and is
why you should try and avoid if at all possible.
John