Replace write in fstream

J

Jon Slaughter

I'm trying to replace some bytes in a file using fstream but all I seem to
be able to do is append or 0 the file then write... Basicaly I just need to
replace the first 512 bytes of the file with some buffer(but eventually I
will need to replace random blocks in the file at different locations). Is
there anyway to do this efficiently using fstream? (I know I can duplicate
the file, but it seems a waste to rewrite the same data for no reason).

Thanks.
 
J

Jon Slaughter

Jon Slaughter said:
I'm trying to replace some bytes in a file using fstream but all I seem to
be able to do is append or 0 the file then write... Basicaly I just need
to replace the first 512 bytes of the file with some buffer(but eventually
I will need to replace random blocks in the file at different locations).
Is there anyway to do this efficiently using fstream? (I know I can
duplicate the file, but it seems a waste to rewrite the same data for no
reason).

Thanks.

my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);

VM_Drive.seekp(0, ios_base::beg);

char buf[512];

for(int i = 0; i<512;i++) buf = 2;

VM_Drive.write(buf, 512);

VM_Drive.close();



I've tried ios:app which will append the data, but won't replace.



the ios::eek:ut seems to be working exactly as ios::trunc. It resizes the file
to 0 when I open.
 
J

Jonathan Turkanis

Jon said:
Jon Slaughter said:
I'm trying to replace some bytes in a file using fstream but all I
seem to be able to do is append or 0 the file then write... Basicaly
I just need to replace the first 512 bytes of the file with some
buffer(but eventually I will need to replace random blocks in the
file at different locations). Is there anyway to do this efficiently
using fstream? (I know I can duplicate the file, but it seems a
waste to rewrite the same data for no reason).

Thanks.

my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);

try ios::in | ios::eek:ut | ios::binary

Jonathan
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon,

Jon said:
I'm trying to replace some bytes in a file using fstream but all I seem to
be able to do is append or 0 the file then write... Basicaly I just need
to replace the first 512 bytes of the file with some buffer(but eventually
I will need to replace random blocks in the file at different locations).
Is there anyway to do this efficiently using fstream? (I know I can
duplicate the file, but it seems a waste to rewrite the same data for no
reason).

Thanks.


my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);
This is fine as far as it goes, however, if you have a look at
streambuf.h in your STL, you will see a flag called nocreate, and
another one called noreplace. Both of these do about what you would
expect. As for the binary flag, this is only needed on those platforms
which make a distinction between the two types of files (i.e. on MS
windows).
VM_Drive.seekp(0, ios_base::beg);

char buf[512];

for(int i = 0; i<512;i++) buf = 2;

VM_Drive.write(buf, 512);

VM_Drive.close();
[snip]


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB0rwupxCQXwV2bJARAhmYAJ9h68lCfdSPEENCttHL1JMeXe7MAwCZAeVr
kCZ5Xn7M2F0hyFOwLsfYEms=
=zimQ
-----END PGP SIGNATURE-----
 
J

Jonathan Mcdougall

Evan said:
Jon,

Jon said:
I'm trying to replace some bytes in a file using fstream but all I seem to
be able to do is append or 0 the file then write... Basicaly I just need
to replace the first 512 bytes of the file with some buffer(but eventually
I will need to replace random blocks in the file at different locations).
Is there anyway to do this efficiently using fstream? (I know I can
duplicate the file, but it seems a waste to rewrite the same data for no
reason).

my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);

This is fine as far as it goes, however, if you have a look at
streambuf.h in your STL, you will see a flag called nocreate, and
another one called noreplace. Both of these do about what you would
expect. As for the binary flag, this is only needed on those platforms
which make a distinction between the two types of files (i.e. on MS
windows).

nocreate and noreplace are non standard flags,
prefer not to use them if you can.


Jonathan
 
J

Jon Slaughter

Jonathan Turkanis said:
Jon said:
Jon Slaughter said:
I'm trying to replace some bytes in a file using fstream but all I
seem to be able to do is append or 0 the file then write... Basicaly
I just need to replace the first 512 bytes of the file with some
buffer(but eventually I will need to replace random blocks in the
file at different locations). Is there anyway to do this efficiently
using fstream? (I know I can duplicate the file, but it seems a
waste to rewrite the same data for no reason).

Thanks.

my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);

try ios::in | ios::eek:ut | ios::binary

Jonathan

that doesn't work... it still recreates the file.
 
J

Jon Slaughter

Evan Carew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jon,

Jon said:
I'm trying to replace some bytes in a file using fstream but all I seem
to be able to do is append or 0 the file then write... Basicaly I just
need to replace the first 512 bytes of the file with some buffer(but
eventually I will need to replace random blocks in the file at different
locations). Is there anyway to do this efficiently using fstream? (I know
I can duplicate the file, but it seems a waste to rewrite the same data
for no reason).

Thanks.


my code:

fstream VM_Drive(argv[2], ios::eek:ut | ios::binary);
This is fine as far as it goes, however, if you have a look at streambuf.h
in your STL, you will see a flag called nocreate, and another one called
noreplace. Both of these do about what you would expect. As for the binary
flag, this is only needed on those platforms which make a distinction
between the two types of files (i.e. on MS windows).


I don't understand how these flags are useful? I am trying to open a
pre-existing file and overwrite only part of it. But my code truncates the
file for no reason. (if the file is 1024 bytes and I write 512 bytes, the
"new" file is 512 bytes... and I don't want to recreate the file because it
can be up to several gigs). I'm not sure whats going on, but it seems
ios::eek:ut is acting just like ios::trunc.

(and yes, I'm using windows)
VM_Drive.seekp(0, ios_base::beg);

char buf[512];

for(int i = 0; i<512;i++) buf = 2;

VM_Drive.write(buf, 512);

VM_Drive.close();
[snip]


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFB0rwupxCQXwV2bJARAhmYAJ9h68lCfdSPEENCttHL1JMeXe7MAwCZAeVr
kCZ5Xn7M2F0hyFOwLsfYEms=
=zimQ
-----END PGP SIGNATURE-----
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top