Simple ifstream question

F

flips

OK this is really simple but I can't think what is the best
and most elegant solution.

Say I am getting an int from an ifstream as follows:

int val;
inFile.get(reinterpret_cast<char*>(&val), sizeof(val));

How do I know if this succeeded? It only returns false if
the eof is at the end of the int.
 
M

Mike Wahler

flips said:
OK this is really simple but I can't think what is the best
and most elegant solution.

Say I am getting an int from an ifstream as follows:

int val;
inFile.get(reinterpret_cast<char*>(&val), sizeof(val));

How do I know if this succeeded? It only returns false if
the eof is at the end of the int.

if(!inFile && !inFile.eof())
/* error */

-Mike
 
M

Mike Wahler

flips said:
Are you mad?

Not at all. If what I wrote doesn't help,
perhaps you should express your problem more
clearly.

BTW Please don't top post.

-Mike
 
A

Andreas Kirkeskov Carlsen

Mike Wahler said:
if(!inFile && !inFile.eof())
/* error */

You clearly must have meant:

if(!inFile || inFile.eof())
/* error */

What you wrote isn't meaningful...
(If "!inFile" is true then "!inFile.eof()" willl yield a null pointer
exception!)

/Andreas
 
F

flips

Andreas Kirkeskov Carlsen said:
You clearly must have meant:

if(!inFile || inFile.eof())
/* error */

What you wrote isn't meaningful...
(If "!inFile" is true then "!inFile.eof()" willl yield a null pointer
exception!)

!?!?!

Null pointer exception? inFile isn't even a pointer.
 
T

TaiwanNoWhere

if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough
 
F

flips

TaiwanNoWhere said:
if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough

Quite.

Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.

Is this the same on other people's compilers? Perhaps I need
to update the verion of STL I'm using.
 
T

Thomas Wintschel

flips said:
Quite.

Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.

Is this the same on other people's compilers? Perhaps I need
to update the verion of STL I'm using.

Have you tried inFile.good()?

Tom
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

flips escribió:
Anyway, the point of my original post was that, if the file is
say 3 bytes long, and I read 4 (to read an int), no error occurs.

inFile is true AND inFile.eof() is false.

inFile.gcount () tell you the number of char readed.

Regards.
 
M

Mike Wahler

Andreas Kirkeskov Carlsen said:
You clearly must have meant:

if(!inFile || inFile.eof())
/* error */

What you wrote isn't meaningful...

Sure it is. Its meaning is:
If the stream is in a fail state, and end of stream has not
been reached, then an error has occurred. What do you think
it means?
(If "!inFile" is true then

then the stream is in fail state.
"!inFile.eof()" willl yield a null pointer
exception!)

Regardless of the state of the stream, "!inFile.eof()" will
yeild true if end of stream has not been reached, false if
it has.

There is nothing to do with pointers or exceptions at all
in what I wrote.

What C++ book(s) have you been reading?

-Mike
 
M

Mike Wahler

TaiwanNoWhere said:
if(!inFile && !inFile.eof())
/* error */

why do you type (!inFile && !inFile.eof())
I do not understand the purpose of (!inFile)
I thought that (!inFile.eof()) is enough

End of stream will cause failbit (as well as eofbit)
to be set. I check eof() to distinguish between end of
stream and a 'real' error.

-Mike
 

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

Similar Threads

ifstream >> string??? 1
Pythen question 0
ifstream 1
ifstream 5
A simple form question 2
Help with Loop 0
ifstream ofstream ? 11
[ifstream] Double reading of the last character. 2

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top