473,394 Members | 1,794 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,394 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 22531
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.