OSDev.org https://forum.osdev.org/ |
|
The strangest behavior with C++ iostream I've ever seen https://forum.osdev.org/viewtopic.php?f=13&t=56274 |
Page 2 of 2 |
Author: | Ethin [ Thu May 12, 2022 4:07 pm ] |
Post subject: | Re: The strangest behavior with C++ iostream I've ever seen |
nullplan wrote: Ethin wrote: But the check to see if c != 0 or c == 0 below that negates that need. Well no. According to cppreference: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). 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:Code: char c; That ought to do it.while (in.get(c)) Thanks for all the help ![]() |
Page 2 of 2 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |