468,462 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

appending data to binary file fstream

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::out | 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.
Dec 30 '07 #1
10 20873
rory wrote:
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::out | 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::out | ios_base::app
if you use fostream, then "out" is by default.
Dec 31 '07 #2
On Dec 31, 3:25 am, Barry <dhb2...@gmail.comwrote:
rory wrote:
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::out | 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::out | 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.
Dec 31 '07 #3
rory wrote:
On Dec 31, 3:25 am, Barry <dhb2...@gmail.comwrote:
>rory wrote:
>>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::out | 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::out | 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,
have you check if you opened the file successfully?
Dec 31 '07 #4
On Dec 31, 11:19 am, Barry <dhb2...@gmail.comwrote:
rory wrote:
On Dec 31, 3:25 am, Barry <dhb2...@gmail.comwrote:
rory wrote:
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::out | 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::out | 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,

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.
Dec 31 '07 #5
On Dec 30, 7:52 pm, rory <rorywa...@gmail.comwrote:
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::out | 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::ofstream f(
"test.txt",
std::ios::out | 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::out, it shouldn't be, unless you explicitly request
truncation.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Dec 31 '07 #6
On Dec 31 2007, 3:36 pm, James Kanze <james.ka...@gmail.comwrote:
On Dec 30, 7:52 pm,rory<rorywa...@gmail.comwrote:
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::out | 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::ofstream f(
"test.txt",
std::ios::out | 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::out, it shouldn't be, unless you explicitly request
truncation.

--
James Kanze (GABI Software) email:james.ka...@gmail.com
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.
Jan 2 '08 #7
On Jan 2, 1:06 pm, rory <rorywa...@gmail.comwrote:
On Dec 31 2007, 3:36 pm, James Kanze <james.ka...@gmail.comwrote:
On Dec 30, 7:52 pm,rory<rorywa...@gmail.comwrote:
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::out | 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::ofstream f(
"test.txt",
std::ios::out | 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::out, it shouldn't be, unless you explicitly request
truncation.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
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.
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.
Jan 2 '08 #8
On Jan 2, 1:06 pm, rory <rorywa...@gmail.comwrote:
On Dec 31 2007, 3:36 pm, James Kanze <james.ka...@gmail.comwrote:
On Dec 30, 7:52 pm,rory<rorywa...@gmail.comwrote:
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::out | 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::ofstream f(
"test.txt",
std::ios::out | 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::out, it shouldn't be, unless you explicitly request
truncation.
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jan 3 '08 #9
On Jan 2, 2:34 pm, rory <rorywa...@gmail.comwrote:
On Jan 2, 1:06 pm, rory <rorywa...@gmail.comwrote:
[...]
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jan 3 '08 #10
On Jan 3, 9:47 am, James Kanze <james.ka...@gmail.comwrote:
On Jan 2, 2:34 pm, rory <rorywa...@gmail.comwrote:
On Jan 2, 1:06 pm, rory <rorywa...@gmail.comwrote:

[...]
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:james.ka...@gmail.com
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.
Jan 3 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by J. Campbell | last post: by
9 posts views Thread by Use*n*x | last post: by
1 post views Thread by Gert Conradie | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.