| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"flips" |
| Date: |
22 Nov 2003 06:49:39 PM |
| Object: |
Simple ifstream question |
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.
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: Simple ifstream question |
22 Nov 2003 06:55:38 PM |
|
|
"flips" <flips@flops.comm> wrote in message
news:bpp072$d6l$1@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
.
|
|
|
| User: "Andreas Kirkeskov Carlsen" |
|
| Title: Re: Simple ifstream question |
22 Nov 2003 10:25:40 PM |
|
|
"Mike Wahler" <mkwahler@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
.
|
|
|
| User: "flips" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 01:50:19 AM |
|
|
"Andreas Kirkeskov Carlsen" <akc@daimi.au.dk> wrote in message
news:bppckf$a09$1@news.net.uni-c.dk...
"Mike Wahler" <mkwahler@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.
.
|
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 03:12:45 PM |
|
|
"Andreas Kirkeskov Carlsen" <akc@daimi.au.dk> wrote in message
news:bppckf$a09$1@news.net.uni-c.dk...
"Mike Wahler" <mkwahler@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
.
|
|
|
|
|
| User: "flips" |
|
| Title: Re: Simple ifstream question |
22 Nov 2003 07:36:30 PM |
|
|
Are you mad?
"Mike Wahler" <mkwahler@mkwahler.net> wrote in message
news:eATvb.11201$n56.9476@newsread1.news.pas.earthlink.net...
"flips" <flips@flops.comm> wrote in message
news:bpp072$d6l$1@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
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: Simple ifstream question |
22 Nov 2003 08:18:25 PM |
|
|
"flips" <flips@flops.comm> wrote in message
news:bpp2uu$e1l$1@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
.
|
|
|
|
|
| User: "TaiwanNoWhere" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 02:28:35 AM |
|
|
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
.
|
|
|
| User: "Mike Wahler" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 03:13:40 PM |
|
|
"TaiwanNoWhere" <taiwannowhere@netscape.net> wrote in message
news:1648d47a.0311230028.2e3bcab3@posting.google.com...
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
.
|
|
|
|
| User: "flips" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 03:13:07 AM |
|
|
"TaiwanNoWhere" <taiwannowhere@netscape.net> wrote in message
news:1648d47a.0311230028.2e3bcab3@posting.google.com...
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.
.
|
|
|
| User: "Thomas Wintschel" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 03:24:38 AM |
|
|
"flips" <flips@flops.comm> wrote in message
news:bpptn3$m95$1@news.freedom2surf.net...
"TaiwanNoWhere" <taiwannowhere@netscape.net> wrote in message
news:1648d47a.0311230028.2e3bcab3@posting.google.com...
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
.
|
|
|
|
| User: "=?iso-8859-1?Q?Juli=E1n?= Albo" |
|
| Title: Re: Simple ifstream question |
23 Nov 2003 04:30:56 AM |
|
|
flips escribi=F3:
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.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|