Strange stream behaviour

Roman Belenov rbelenov@yandex.ru
Mon Oct 8 00:24:00 GMT 2001


Nathan Myers <ncm@nospam.cantrip.org> writes:

> On Sat, Oct 06, 2001 at 06:35:57PM +0400, Roman Belenov wrote:
> > I've found that simple copy program
> >   #include <iostream>
> >   int main()
> >   { int ch; while( (ch = std::cin.get()) != EOF ) std::cout.put(ch); }
> > 
> > duplicates pieces from input, first occurence being about
> > position 3000 in the input stream.
> 
> There is no reason to expect the program to work.  In particular, the 
> function cin.get() can never return EOF under any circumstances.  EOF 
> is an int constant, where cin.get() can only return character
> values.

According to ISO/IEC 14882, 27.6.1.1 and 27.6.1.3, istream::get()
return type is char_traits<char>::int_type (since istream is
basic_istream<char>) and it does return char_traits<char>::eof() if
input character is not available. According to 21.1.3.1,
char_traits<char>::eof() returns EOF

> The loop termination condition for such a program must check the 
> stream state, not the result of cin.get().

Checking stream state is also an option, but checking return value
semms to be equally correct.


Anyway, the problem I mentioned doesn't relate to loop condition -
duplicated pieces of text appear throught the whole text, not get
appended to the end of it.

-- 
 							With regards, Roman.



More information about the Libstdc++ mailing list