Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help! How do I use ifstream to read binary files?

33 views
Skip to first unread message

Craig Garrett

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

I am trying to use ifstream to read binary files, but just after I read the
first variable (its an unsigned short int), the stream closes or something
and refuses to read any more variables.

Here is part of my code:

ifstream cellhistory("br3d4c.ucn", ios::binary | ios::in);
.
.
.
unsigned short int buffer1 = 5;
unsigned int NTRANS = 5;

cellhistory >> buffer1;
if (!cellhistory) {printf("Error\n");}
cellhistory >> NTRANS;

This code puts a value of 0 in buffer1 and prints "Error" on the screen.
Also, if I put the if statement BEFORE I try to read buffer1, then the if
statement does NOT fire, "Error" does not get printed. I have never used
ifstream for binary input before, what am I doing wrong?

Craig Garrett


Craig Garrett

unread,
Jun 17, 1998, 3:00:00 AM6/17/98
to

Never mind, I found an example:

cellhistory.read ( (char*) &buffer1, sizeof(buffer1) );

this seems to work


Keith Davies

unread,
Jun 24, 1998, 3:00:00 AM6/24/98
to

On Wed, 17 Jun 1998 22:34:28 GMT, "Craig Garrett"
<cgar...@siscom.net> wrote:

>ifstream cellhistory("br3d4c.ucn", ios::binary | ios::in);
>.
>.
>.
> unsigned short int buffer1 = 5;
> unsigned int NTRANS = 5;
>
> cellhistory >> buffer1;
> if (!cellhistory) {printf("Error\n");}
> cellhistory >> NTRANS;
>
>This code puts a value of 0 in buffer1 and prints "Error" on the screen.
>Also, if I put the if statement BEFORE I try to read buffer1, then the if
>statement does NOT fire, "Error" does not get printed. I have never used
>ifstream for binary input before, what am I doing wrong?

The normal insertion and extraction operators (<< and >>) still
perform ASCII I/O, even if the file is declared binary. The
ios::binary flag merely indicates, among other things, that normal EOF
characters (^Z on a PC) are to be ignored.

In order to perform binary I/O you should use ostream::write() and
istream::read(). Alternatively, you could use binary stream classes
instead of the normal iostreams. My BitStreams library is still
available at http://www.pinc.com/~kdavies Be warned, however, that
although it works (it performs big endian binary I/O in a portable
fashion) I haven't reviewed it in a year and a half; it probably needs
some work (such as support for little endian I/O).

>Never mind, I found an example:
>
>cellhistory.read ( (char*) &buffer1, sizeof(buffer1) );
>
>this seems to work

It will under the following conditions:
1. you are on a machine with the same byte ordering (big vs little
endian) - as soon as you change to a different type of machine you
will have to fix code.
2. buffer1 stays the same size - if you were using int instead of
short you would get hit, and still might if someone changes the
size of short. The only rules about integer sizes are that a short
must be at least 16 bits, a long must be at least 32 bits and at
least as long as a short, and an int must be at least as big as a
short and no bigger than a long.

I sometimes use something like this in hack code or simple tests, but
nothing that will see production. You are probably better off using a
library designed for binary I/O.

Keith
-- [posted and mailed]
Keith Davies
kjda...@ii.ca

0 new messages