 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
f.anonymous@gmail.com Guest
|
Posted: Mon Oct 24, 2005 9:10 am Post subject: input stream state |
|
|
Hi,
for an input stream s, can the expression:
!s.bad() && !s.eof() && s.fail()
ever be true?
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Tue Oct 25, 2005 11:20 am Post subject: Re: input stream state |
|
|
[email]f.anonymous (AT) gmail (DOT) com[/email] wrote:
| Quote: | for an input stream s, can the expression:
!s.bad() && !s.eof() && s.fail()
ever be true?
|
If only the failbit is set. Typically, this will be the case if
there was a format error on input, e.g. you tried to read into
an int, and the stream contained "abcd". (Note that in some
cases of format error, it isn't 100% guaranteed that eof is not
set. But this is about the closest you can come to detecting a
format error.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
John Potter Guest
|
Posted: Tue Oct 25, 2005 1:21 pm Post subject: Re: input stream state |
|
|
On 24 Oct 2005 05:10:34 -0400, [email]f.anonymous (AT) gmail (DOT) com[/email] wrote:
| Quote: | for an input stream s, can the expression:
!s.bad() && !s.eof() && s.fail()
ever be true?
|
Sure.
#include <cassert>
#include <sstream>
int main () {
std::istringstream s("Fail");
int x;
s >> x;
assert(!s.bad() && !s.eof() && s.fail());
}
John
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
f.anonymous@gmail.com Guest
|
Posted: Wed Oct 26, 2005 1:18 pm Post subject: Re: input stream state |
|
|
John Potter wrote:
| Quote: | On 24 Oct 2005 05:10:34 -0400, [email]f.anonymous (AT) gmail (DOT) com[/email] wrote:
for an input stream s, can the expression:
!s.bad() && !s.eof() && s.fail()
ever be true?
Sure.
#include <cassert
#include
int main () {
std::istringstream s("Fail");
int x;
s >> x;
assert(!s.bad() && !s.eof() && s.fail());
}
|
Ah, I see - thanks.
So, does this condition only crop up on format errors? How about:
template <typename input_stream>
int test(input_stream &s) {
char c;
s.get(c);
assert(!(!s.bad() && !s.eof() && s.fail()));
}
Can this assert ever fire?
Thanks.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|