View Full Version : C++ File I/O

5th August 2007, 04:56 AM
I need to open an output file stream and change a block of data in the middle of the file. Opening the stream with ios::binary alone will cause the previous contents to be deleted, while opening with ios::binary and ios::app will preserve the contents while rendering seekp() useless.

How do I go about changing that block of data without resorting to reading the entire file into a vector, one block of data at a time, then finding said block and changing it before writing the entire vector back to a file? While the vector method works when the file is small, I'd rather not use it when the file containing the data in question becomes larger.

5th August 2007, 07:21 AM
Ermmm, I dont know C++, But Google returns: http://www.cplusplus.com/doc/tutorial/files.html

ios::ate -> Set the initial position at the end of the file.
If this flag is not set to any value, the initial position is the beginning of the file.

Prohaps setting the ios::ate=pos may work


5th August 2007, 06:28 PM
A quick test of it shows the same results as using ios::binary alone. It appears ios::ate does not preserve the file's contents, which is odd since it sets the read/write pointer to eof. As far as I can tell, only when using ios::app while opening an output stream will cause the contents to be preserved, though it also has the effect of setting the write pointer to eof before every write/put.