J
Jon Slaughter
I posted this question before but did not get any answers that worked.
I'm trying to write to the first part of a file using fstream without
messing with the rest of the file. I'm opening the file using ios:
ut |
ios::binary, but when I write to the file using the write member, the file
is truncated first.
For example, if I have a 1gig file and I want to only rewrite the first 100
bytes,
fstream f("some 1gig file", ios:
ut || ios::binary);
f.seekp(0, ios_base::beg);
char buf[100];
f.write(buf,100);
f.close();
then the file is only 100 bytes that contains buf. I want the file to be 1
gig but the first 100 bytes correspond to buf. If C++ can't do this, then
this is pretty stupid since its very inefficient(since I would have to copy
the rest of the file, which can be several gigs when I just need to modify a
very very small portion of it). I've tried also to use ios::ape but it has
same effect. I have no problem adding to the end of the file, but when I
seek to the begining to write is when it truncates everything after the
write.
I'm trying to write to the first part of a file using fstream without
messing with the rest of the file. I'm opening the file using ios:
ios::binary, but when I write to the file using the write member, the file
is truncated first.
For example, if I have a 1gig file and I want to only rewrite the first 100
bytes,
fstream f("some 1gig file", ios:
f.seekp(0, ios_base::beg);
char buf[100];
f.write(buf,100);
f.close();
then the file is only 100 bytes that contains buf. I want the file to be 1
gig but the first 100 bytes correspond to buf. If C++ can't do this, then
this is pretty stupid since its very inefficient(since I would have to copy
the rest of the file, which can be several gigs when I just need to modify a
very very small portion of it). I've tried also to use ios::ape but it has
same effect. I have no problem adding to the end of the file, but when I
seek to the begining to write is when it truncates everything after the
write.