By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 839 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Simple ifstream question

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"flips" <fl***@flops.comm> wrote in message
news:bp**********@news.freedom2surf.net...
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
Jul 22 '05 #2

P: n/a
Are you mad?

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:eA******************@newsread1.news.pas.earth link.net...

"flips" <fl***@flops.comm> wrote in message
news:bp**********@news.freedom2surf.net...
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

Jul 22 '05 #3

P: n/a

"flips" <fl***@flops.comm> wrote in message
news:bp**********@news.freedom2surf.net...
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
Jul 22 '05 #4

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote:

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
Jul 22 '05 #5

P: n/a
"Andreas Kirkeskov Carlsen" <ak*@daimi.au.dk> wrote in message
news:bp**********@news.net.uni-c.dk...
"Mike Wahler" <mk******@mkwahler.net> wrote:

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!)


!?!?!

Null pointer exception? inFile isn't even a pointer.
Jul 22 '05 #6

P: n/a
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
Jul 22 '05 #7

P: n/a
"TaiwanNoWhere" <ta***********@netscape.net> wrote in message
news:16**************************@posting.google.c om...
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.
Jul 22 '05 #8

P: n/a

"flips" <fl***@flops.comm> wrote in message
news:bp**********@news.freedom2surf.net...
"TaiwanNoWhere" <ta***********@netscape.net> wrote in message
news:16**************************@posting.google.c om...
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.


Have you tried inFile.good()?

Tom
Jul 22 '05 #9

P: n/a
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.
Jul 22 '05 #10

P: n/a

"Andreas Kirkeskov Carlsen" <ak*@daimi.au.dk> wrote in message
news:bp**********@news.net.uni-c.dk...
"Mike Wahler" <mk******@mkwahler.net> wrote:

if(!inFile && !inFile.eof())
/* error */
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
Jul 22 '05 #11

P: n/a

"TaiwanNoWhere" <ta***********@netscape.net> wrote in message
news:16**************************@posting.google.c om...
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

Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.