C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

input stream state

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
f.anonymous@gmail.com
Guest





PostPosted: Mon Oct 24, 2005 9:10 am    Post subject: input stream state Reply with quote



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





PostPosted: Tue Oct 25, 2005 11:20 am    Post subject: Re: input stream state Reply with quote



[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





PostPosted: Tue Oct 25, 2005 1:21 pm    Post subject: Re: input stream state Reply with quote



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





PostPosted: Wed Oct 26, 2005 1:18 pm    Post subject: Re: input stream state Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.