I'm trying to use C++ to write an entry in a specific place in a file
so basicly I have
ofstream ofs("file.dat", ios::binary | ios::app);
ofs.seekp(220, ios::beg);
ofs.write((char *)&i, sizeof(i));
But no matter what I do it always write at the end of the file.
I suppose this is related to iso::app
because according to the documentation
app (append) Set the stream's position indicator to the end of the stream before each output operation
But if I use ate or nothing it always erases the content of the file.
Any help would be great :)
Yes, it's the ios::app
that's causing this behaviour. Replace it with ios::in | ios::out
.
edit: It wasn't clear from your question, but your comments suggest that you're trying to insert data in the middle of the file, rather than overwrite a portion of the file. If that is indeed the case, you pretty much have to use a second file for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With