I am making a program in which i need to append the file from the beginning. so i am opening the file like this
wFile.open("sat.txt",ios::ate| ios::out | ios::in);
moving the pointer to the beginning

but when i try add data it is overwriting the data. Is there any way that it doesn't overwrite the data.
Thanks for the help

7 Years
Discussion Span
Last Post by ArkM

Well fptr.open(<filename>,ios::out|ios::app) helps you to append the data to the end of the file. But I don't think you can append it from the beginning of the file because the concept of append defines itself as adding contents to the end.

You can always do this :
1>Create another temporary file say temp.txt
2>Write the contents of your main file to this temp file.
3>Then open your main file in fptr.open(<filename>,ios::out);
4>Write into the file what ever you wanted to "append" earlier.Now as you know the entire file contents gets deleted and only the thing you just wrote is in the beginning of the file.
5>Re open your main file in fptr.open(<filename>,ios::out|ios::app) mode.
6>Write the contents of temp file to this file.

Even though a cumbersomely long process your objective is achieved.If someone knows a direct api for the above purpose I too would like to learn :).


If you wrote a text line on a paper sheet, you can't insert anything in this line without a rubber and rewriting or a glue and scissors. A file storage looks like a paper, however it's impossible to cut then to glue up your hard disk tracks.

More safe procedure:
1. Write new data to the new file, don't close it.
2. Open the old file and write (append) its contents to the new one.
3. Close both files.
4. Rename or remove the old file.
5. Rename the new file to the old file name.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.