Re: The strangest behavior with C++ iostream I've ever seen
Posted: Thu May 12, 2022 4:07 pm
Thanks for all the helpnullplan wrote:Well no. According to cppreference:Ethin wrote:But the check to see if c != 0 or c == 0 below that negates that need.If I read that right, the value of c is undefined on failure. In any case, it appears that operator>> is overkill anyway, since you only want to extract single characters from the stream. So one possible solution would be to ditch it and instead use get() and unget(). get() without arguments returns the next character or EOF, but alternatively you can pass a character variable as reference, and then the return value can be converted to bool to get the state, so in effect:https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2 wrote:Behaves as an FormattedInputFunction. After constructing and checking the sentry object, which may skip leading whitespace, extracts a character and stores it to ch. If no character is available, sets failbit (in addition to eofbit that is set as required of a FormattedInputFunction).That ought to do it.Code: Select all
char c; while (in.get(c))
