C++ equivalent for fread() in ANSI C

W

wang

Hi all,
I'd like to read from an ifstream in binary mode a certain length of
characters into a buffer, just as fread() in ANSI C does. How to do it
in C++? Many thanks in advance!
kwwang
 
F

Francesco S. Carta

Hi all,
I'd like to read from an ifstream in binary mode a certain length of
characters into a buffer, just as fread() in ANSI C does. How to do it
in C++? Many thanks in advance!
kwwang

This should do the same work:

http://www.cppreference.com/wiki/io/read

Having a look at a reference like that can help figuring out at a glance
what facilities are available for any given feature.
 
V

Vaclav Haisman

wang wrote, On 8.8.2010 18:14:
Hi all,
I'd like to read from an ifstream in binary mode a certain length of
characters into a buffer, just as fread() in ANSI C does. How to do it
in C++? Many thanks in advance!
Use istream::read().
 
F

Francesco S. Carta

This should do the same work:

http://www.cppreference.com/wiki/io/read

Having a look at a reference like that can help figuring out at a glance
what facilities are available for any given feature.

The example given in the referenced page:

struct {
int height;
int width;
} rectangle;

input_file.read( (char *)(&rectangle), sizeof(rectangle) );
if( input_file.bad() ) {
cerr << "Error reading data" << endl;
exit( 0 );
}

is not the best one around, since it makes use of C-style casts instead
of the more explicit C++ casts, and also because it assumes that the
writing process and the reading process share the same exact
representation of the "rectangle" struct, thus making it non portable
and fallible.

Please don't take the above example as good style, because it's not. I
should have looked closely before posting that link.
 
T

Thomas J. Gritzan

Am 08.08.2010 18:30, schrieb Francesco S. Carta:
The example given in the referenced page:

struct {
int height;
int width;
} rectangle;

input_file.read( (char *)(&rectangle), sizeof(rectangle) );
if( input_file.bad() ) {
cerr << "Error reading data" << endl;
exit( 0 );
}

is not the best one around, since it makes use of C-style casts instead
of the more explicit C++ casts, and also because it assumes that the
writing process and the reading process share the same exact
representation of the "rectangle" struct, thus making it non portable
and fallible.

And you shouldn't use istream::bad(). From the reference above:

"Note: fatal errors do not normally occur. Even a failure to open a file
is not a fatal error."

Usually, you should use
if (stream) or if (!!stream)
after a reading attempt to check if it was successful.
 
T

Thomas J. Gritzan

Am 09.08.2010 13:59, schrieb Juha Nieminen:
Isn't the latter needlessly obfuscated?

Yes, but some people do it to explicitly convert to bool.
 
F

Francesco S. Carta

Am 09.08.2010 13:59, schrieb Juha Nieminen:

Yes, but some people do it to explicitly convert to bool.

I find it's unnecessary in this case (and not only in this case),
because the "if" construct would implicitly convert it to bool in anyway.

That trick is necessary only if, for example, you need to pass a stream
as a bool to some overloaded function that could either take a bool or a
stream - and since the perfect match would prefer the version taking a
stream, you need to explicitly convert it in order to call the version
taking a bool - although, even in that case, I would write
"bool(stream)" instead of "!!stream", to clearly state my intention.
 
J

James Kanze

Am 09.08.2010 13:59, schrieb Juha Nieminen:
Yes, but some people do it to explicitly convert to bool.

In which case, it's really obfuscation. If you want to
explicitly convert to bool, the solution is
static_cast<bool>(stream).

In general, I don't like the idea of objects implicitly
converting to bool. But in the case of the streams, there's
really not much choice.
 
A

Alf P. Steinbach /Usenet

* James Kanze, on 09.08.2010 19:51:
In which case, it's really obfuscation. If you want to
explicitly convert to bool, the solution is
static_cast<bool>(stream).

It's an idiom, the C and C++ shorthand for conversion to bool.

In general, I don't like the idea of objects implicitly
converting to bool. But in the case of the streams, there's
really not much choice.

In Python:

for line in stream:
print( line )

He he. :)


Cheers,

- Alf
 
A

Alf P. Steinbach /Usenet

* James Kanze, on 10.08.2010 09:56:
It's an idiom that I've never seen in well written C or C++.

Good idea to start using it with MSVC. ;-) (Since it spews out silly-warnings,
if all things about /performance/, on implicit conversions to bool (you've
probably worked mostly with more reasonable compilers, I guess)).

Which means...? (I don't know python, so I don't see any
relevance with what we're discussing.)

It extracts each line from a stream.

The inside is ugly, an exception is thrown on end of stream and caught by the
loop construct. But the details don't matter, it could have worked in some
different way. All that an implicit conversion to bool is about is convenience
of notation, and mostly for the case of iteration, and nothing can be more
convenient than built-in language support for iterating over a collection. :)

Happily C++0x will have such a construct but it doesn't help with the getting
rid of the conversions...


Cheers,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top