The typical way to handle such things is to follow this order:
1. Rename the original file to something temporary (such as "
foo.txt" to "
foo.txt.bak")
2. Create a new file named as the original
3. Open both files
4. Read the original file, modify, write the new file.
5. Close both files.
6. Delete the original file (optional)
Some steps may be combined.
On Linux (and with MinGW), there exists the
trunctate() and
ftrunctate() functions:
http://linux.die.net/man/2/truncate
On Windows, you can use the
SetEndOfFile() function:
http://msdn.microsoft.com/en-us/library/aa365531(VS.85).aspx
All this is very OS-dependant though, especially if you are working with >2GB files.
If you know you are working with just a very small file, you can load it into a
std::
deque of strings, modify it in memory, then just rewrite the file.
Hope this helps.