Open a file in binary mode

Open a file in binary mode

Post by Hatzigiann » Wed, 14 Nov 2007 17:12:44


I have written the following code in DEV C++ but the produced file is a
normal text file (you can type it in command line). I was expecting that the
numeric values would have written in their binary internal representation
and not as pure text.



What have I done wrong?



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

main ()
{
ofstream myfile;
myfile.open("datafile",ios::out|ios::binary);
myfile<<123<<34<<12.123<<"start"<<endl;
myfile<<"end";
myfile.close();
}
 
 
 

Open a file in binary mode

Post by Ian Collin » Wed, 14 Nov 2007 17:27:49


This sets the file mode, not the stream.

--
Ian Collins.

 
 
 

Open a file in binary mode

Post by Hatzigiann » Wed, 14 Nov 2007 19:30:21

and how can I have a binary stream?!

? "Ian Collins" < XXXX@XXXXX.COM > ?????? ??? ??????
 
 
 

Open a file in binary mode

Post by Gianni Mar » Wed, 14 Nov 2007 19:50:10


Usually you use the read() and write() methods to do that.

It can be tricky to get it right, what exactly are you trying to do ?
 
 
 

Open a file in binary mode

Post by Hatzigiann » Wed, 14 Nov 2007 20:39:45


was trying to demonstrate the difference between the two modes. I cannot
see any since the output is exactly the same either in text or binary mode,
in the given code

? "Gianni Mariani" < XXXX@XXXXX.COM > ?????? ??? ??????
 
 
 

Open a file in binary mode

Post by Pete Becke » Wed, 14 Nov 2007 22:26:24

On 2007-11-13 06:39:45 -0500, "Hatzigiannakis Nikos" < XXXX@XXXXX.COM > said:




Inserters do formatting, regardless of whether the stream is in binary
or text mode. write() outputs binary data, regardless of whether the
stream is in binary or text mode.

The difference between the two modes is whether the stream translates
"special" characters. In particular, '\n' represents a newline, and in
text mode the stream writes whatever is appropriate on the system where
it's running to represent the end of a line. In binary mode it's just
the value \x0D.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
 
 
 

Open a file in binary mode

Post by red floy » Wed, 14 Nov 2007 23:13:58


Uh, Pete, isn't that '\x0a'? I thought '\r' was '\x0d'.
 
 
 

Open a file in binary mode

Post by Pete Becke » Thu, 15 Nov 2007 01:18:47

On 2007-11-13 09:13:58 -0500, red floyd < XXXX@XXXXX.COM > said:



You're probably right. But on further reflection, it's neither. For
some reason I thought the standard specified the value of '\r' and
'\n'. But that's a hallucination. The value depends on the compiler.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
 
 
 

Open a file in binary mode

Post by red floy » Thu, 15 Nov 2007 01:48:18


That makes sense, since it would be dependent on the execution character
set (0x0a and 0x0d are ASCII, but EBCDIC would probably be quite different).
 
 
 

Open a file in binary mode

Post by James Kanz » Thu, 15 Nov 2007 19:18:49

n Nov 13, 2:26 pm, Pete Becker < XXXX@XXXXX.COM > wrote:





And when writing to a file, the embedded locale does code
translation, regardless of the mode or whether you use inserts
or unformatted output functions. (A name which I detest. All
output has a format. If you use write() carelessly, you may end
up not knowing the format, but it does have a format.)


There's a lot more to it than that. On some systems, different
files types might be used, and it may not be possible to open in
text mode a stream written in binary, and vice versa. On other
systems (including all Unix and Unix-like systems), there's
absolutely no difference, the two modes are indistiguishable.


Internally.


Again, it's more subtle than that. According to the (C)
standard: "A text stream is an ordered sequence of characters
composed into lines, each line consisting of zeor or more
characters plus a terminating new-line character." A text stream
is only guaranteed to work if the data written consists only of
printing characters, '\t' and '\n', no '\n' is immediatly
preceded by a space character, and the last character is a '\n'.
You cannot simply write arbitrary binary data to a text stream.
And what is physically on the disk may be different from what
you would see if you dumped the buffer from memory. (At least
one implementation of C for an IBM mainframe used ASCII
internally, and translated to EBCDIC when it output to disk.
Lines in text files were mapped to fixed length records on the
disk, with each line spaced padded to the record
length---trailing spaces were stripped on input---and no
characters whatever for the '\n'. The standard was carefully
designed to allow such implementations.)

In practice: Unix treats text mode as binary. (Unix also
requires an encoding derived from ASCII, with '\n' being
represented as 0x0A, '\r' as 0x0D, '\t' as 0x09, etc. This is a
Posix requirement, however, not a C/C++ one.) Windows maps '\n'
to the two byte sequence 0x0D, 0x0A, and recognizes 0x1A as an
end of file on input.

A binary stream just dumps whatever you give it to the output
(modulo code translation by the embedded locale). On the other
hand, it's not required to have a reliable end of file: you may
read more than you wrote (but the additional bytes are
guaranteed to be 0. This is a sop to some older OS's (CP/M, but
I think some DEC OS's as well), which maintained the file size
as a number of sectors, not bytes.


Not on my machines. It's been 0x0A on every system I've seen.
(If the machine used EBCDIC internally, it would probably be
0x15.) But most accurately, it's '\n'. Everywhere. If you're
writing text, you use it to indicate the end of the line, and
the system does the rest. If you're writing in binary mode, you
don't use it, directly.

In theory, at least. In practice, as I said, I've never seen an
actual implementation where it wasn't 0x0A.

James Kanze (GABI Software) email: XXXX@XXXXX.COM
Conseils en informatique orient objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sard, 78210 St.-Cyr-l'ole, France, +33 (0)1 30 23 00 34

 
 
 

Open a file in binary mode

Post by Alf P. Ste » Thu, 15 Nov 2007 19:30:37

* James Kanze -> Pete Becker:
[snip]

[snip large pedantic detailing and correction of basics]

Fetching popcorn... :-)

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
 
 

Open a file in binary mode

Post by Pete Becke » Thu, 15 Nov 2007 22:04:34

On 2007-11-14 05:18:49 -0500, James Kanze < XXXX@XXXXX.COM > said:





And since it's not affected by the mode, it's irrelevant to this discussion.


Indeed. But giving long technical explanations to beginners is a good
way to discourage them.


Again, fully detailed explanations are often counterproductive.

This has already been dealt with in two followup messages. But thanks
for piling on.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)