writing to file

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::eek: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::eek: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.
 
R

Rob Williscroft

Jon Slaughter wrote in in
comp.lang.c++:
fstream f("some 1gig file", ios::eek:ut || ios::binary);

fstream f("some 1gig file", ios::eek:ut | ios::binary);

Use: | not || to combine flag values || in this case will give a
value of 1, what "openmode" this might represent on your system
is unknown.

Rob.
 
G

Greg Zavertnik

Jon said:
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::eek: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::eek:ut || ios::binary);

The following should do what you want.

fstream f("some 1gig file", ios::in | ios::eek:ut | ios::binary);

ios::eek:ut by itself will truncate the file. I'm assuming the || was a typo,
but in case not that needs to be a single |.

~greg
 
J

Jon Slaughter

Greg Zavertnik said:
The following should do what you want.

fstream f("some 1gig file", ios::in | ios::eek:ut | ios::binary);

Ok, I tried it before and it wasn't working for some reason, but now it
works...

Thanks.
ios::eek:ut by itself will truncate the file. I'm assuming the || was a
typo, but in case not that needs to be a single |.

yes it was.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,541
Members
48,278
Latest member
OSWDelmar

Latest Threads

Top