appending data to binary file fstream

R

rory

I can't seem to append a string to the end of a binary file. I'm using
the following code:

fstream outFile("test.exe", ios::in | ios::eek:ut | ios::binary |
ios::ate | ios::app)
outFile.write("teststring", 10);
outFile.close();

If I leave out the ios::ate and ios::app modes my string is written to
the start of the file as I'd expect but I want to write the data to
the end of the binary file not the beginning. Can anyone advise me on
what I'm doing wrong? I've also tried using seekp and positioning the
put pointer to the end of the binary file but that doesn't seem to
work either. Any help would be greatly appreciated. Cheers,

Rory.
 
B

Barry

rory said:
I can't seem to append a string to the end of a binary file. I'm using
the following code:

fstream outFile("test.exe", ios::in | ios::eek:ut | ios::binary |
ios::ate | ios::app)
outFile.write("teststring", 10);
outFile.close();

If I leave out the ios::ate and ios::app modes my string is written to
the start of the file as I'd expect but I want to write the data to
the end of the binary file not the beginning. Can anyone advise me on
what I'm doing wrong? I've also tried using seekp and positioning the
put pointer to the end of the binary file but that doesn't seem to
work either. Any help would be greatly appreciated. Cheers,


27.8.1.3/2
Table 92—File open modes

according to the table, "app" never goes with "in"
when "in" and "out" are put together, it means we want to update the old
data, in your case, you didn't mean to.

so use: ios_base::binary | ios_base::eek:ut | ios_base::app
if you use fostream, then "out" is by default.
 
R

rory

27.8.1.3/2
Table 92--File open modes

according to the table, "app" never goes with "in"
when "in" and "out" are put together, it means we want to update the old
data, in your case, you didn't mean to.

so use: ios_base::binary | ios_base::eek:ut | ios_base::app
if you use fostream, then "out" is by default.

Thanks for you reply. I tried that but it still doesn't write anything
to the end of the file. I have now tried almost every combination of
modes I can think of and still now joy. I know if I can't figure it
out that I can easily implement it in C but I've had this problem
before and would love to find a solution. Thanks again for your help,

Rory.
 
B

Barry

rory said:
Thanks for you reply. I tried that but it still doesn't write anything
to the end of the file. I have now tried almost every combination of
modes I can think of and still now joy. I know if I can't figure it
out that I can easily implement it in C but I've had this problem
before and would love to find a solution. Thanks again for your help,

have you check if you opened the file successfully?
 
R

rory

have you check if you opened the file successfully?

Yes I've checked and it seems to be opening correctly, if I take out
the ios::app it will write my string to the file but like I said I
need it at the end. Any other ideas?

Rory.
 
J

James Kanze

I can't seem to append a string to the end of a binary file.
I'm using the following code:
fstream outFile("test.exe", ios::in | ios::eek:ut | ios::binary |
ios::ate | ios::app)
outFile.write("teststring", 10);
outFile.close();
If I leave out the ios::ate and ios::app modes my string is
written to the start of the file as I'd expect but I want to
write the data to the end of the binary file not the
beginning. Can anyone advise me on what I'm doing wrong?

Not really.

std::eek:fstream f(
"test.txt",
std::ios::eek:ut | std::ios::binary | std::ios::app ) ;

works for me, with all of the compilers and libraries I have
access to.

As far as I can tell, the ios::app flag is ignored if the stream
is bidirectional; in such cases, you'll probably have to do a
seekp before each write (and lose the atomicity which ios::app
normally gives). In such cases, however, I'd recommend using
two fstreams: an ifstream for reading and an ofstream for
writing.
I've also tried using seekp and positioning the put pointer to
the end of the binary file but that doesn't seem to work
either.

That should work as well, although it won't be atomic. If you
do that, you must ensure that the file isn't truncated on
opening. Normally, if you open with both ios::in and
ios::eek:ut, it shouldn't be, unless you explicitly request
truncation.
 
R

rory

Not really.

std::eek:fstream f(
"test.txt",
std::ios::eek:ut | std::ios::binary | std::ios::app ) ;

works for me, with all of the compilers and libraries I have
access to.

As far as I can tell, the ios::app flag is ignored if the stream
is bidirectional; in such cases, you'll probably have to do a
seekp before each write (and lose the atomicity which ios::app
normally gives). In such cases, however, I'd recommend using
two fstreams: an ifstream for reading and an ofstream for
writing.


That should work as well, although it won't be atomic. If you
do that, you must ensure that the file isn't truncated on
opening. Normally, if you open with both ios::in and
ios::eek:ut, it shouldn't be, unless you explicitly request
truncation.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thanks James, this works for me too, but only with a text file. When I
try it with a binary file, for example "test.exe", it doesn't write
the string to the end. Have you tried the above code with a binary
file? Sorry for not getting back to you sooner, thank you for your
help.

Rory.
 
R

rory

Thanks James, this works for me too, but only with a text file. When I
try it with a binary file, for example "test.exe", it doesn't write
the string to the end. Have you tried the above code with a binary
file? Sorry for not getting back to you sooner, thank you for your
help.

Rory.

It seems that the viewer I was using to look at the binary data was
not showing the full contents of the file for some reasons. I just did
a simple test and indeed, as yourself and Barry have pointed out, the
methods you suggest do work. Sorry about the noise and thanks again
for the help!

Rory.
 
J

James Kanze

Thanks James, this works for me too, but only with a text file. When I
try it with a binary file, for example "test.exe", it doesn't write
the string to the end.

What do you mean by "binary file" here? I did try it with a
file opened in binary, as above, with no problems.

Note that theoretically (according to the standard), there may
be extra null bytes at the end of a file written in binary.
This isn't the case with any implementations I've encountered
under Windows or Unix, however. (It was the usual case under
CP/M, of course.)
Have you tried the above code with a binary file?

On my usual systems (Linux and Solaris), there is no distinction
between binary files and text files. I did try it on a file
which had been last written in binary mode, however (including
under Windows), and it still worked.

(Note, again theoretically, if the file was written in binary
mode, you can't read it in text mode, and vice versa. In
practice, there's no problem under Unix, and only minor
annoyances under Windows---if you write a 0x1A to the file in
binary mode, for example, it will be taken as end of file if you
read it in text mode.)
 
J

James Kanze

On Jan 2, 1:06 pm, rory <[email protected]> wrote:

[...]
It seems that the viewer I was using to look at the binary data was
not showing the full contents of the file for some reasons.

Maybe it opened the file in text mode. If you're under Windows,
this can result in a premature end of file, and some programs
seem to have problems with new lines written in binary, when
reading in text mode. (The libraries for the C and C++
compilers I'm familiar with all handle new lines written in
binary correctly, but programs which go directly to the OS level
read functions may not.)
 
R

rory

[...]

It seems that the viewer I was using to look at thebinarydata was
not showing the full contents of the file for some reasons.

Maybe it opened the file in text mode. If you're under Windows,
this can result in a premature end of file, and some programs
seem to have problems with new lines written inbinary, when
reading in text mode. (The libraries for the C and C++
compilers I'm familiar with all handle new lines written inbinarycorrectly, but programs which go directly to the OS level
read functions may not.)

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Thanks for all your help on this. I think for now I have things
working the way I need them, hopefully I won't encounter these
problems again! Have a good new year,
Rory.
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top