473,225 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 22450
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
2
by: gauravkhanna | last post by:
Hi All I need some help for the below problem: Scenario We need to send large binary files (audio file of about 10 MB or so) from the client machine (.Net Windows based application, located...
2
abdoelmasry
by: abdoelmasry | last post by:
hi any one can help me plz ?? my code is about opening file as binary then get data from file to edit and rewrite data to file this is the code: #include <cstdlib> #include <iostream>...
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
1
by: Gert Conradie | last post by:
The following code can uplaod text files. When i upload a binary file it fail. I might be: 1) using the wrong Encoding 2) will have to System.Convert.ToBase64String the content of the binary...
6
by: cooldisk | last post by:
Is it possible at all to read a binary file larger than 2GB on a 32- bit system? I tried the following: #include <iostream> #include <fstream> using namespace std; int main(int argc, char...
1
by: jennjgo | last post by:
Hi! ^.^ I am making a program but I can't continue because there's something wrong... #include<iostream> #include<string> #include<fstream> using namespace std;
7
by: vikuba | last post by:
HI I'm new to this forum I'm having a peculiar problem I'm trying to read a 64 bit data binary file I have 5 integer variables a b c d e the problem is I have to read the 64 bit data ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.