Re: VS2005 C++ Express basic_istream::get(buf, size, delim) bug
"Terry G" wrote:.
Is this a compiler bug? Does your C++ compiler produce the same results?
*** Stops reading at the first blank line.***
#include <iostream>
#include <limits>
int main() {
static char Line[1024];
while (std::cin.get(Line, sizeof(Line), '\n')) {
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
std::cout << ':' << Line << std::endl;
}
} // main
<<< Copied from microsoft.public.vc.language. >>>
Mr Tandetnik wrote:
C++ standard about basic_istream::get : "27.6.1.3/8 If the
function stores no characters, it calls setstate(failbit)." Once failbit
is set, the stream object would evaluate to false in boolean context.
That's why the loop should terminate on an empty line.
Thank you, Mr Tandetnik.
g++ behaves the same way.
The only reason it appeared to work was that there were carriage returns
before the newline chars.
When I stripped the \r's, g++ set failbit too.
I'm not sure why basic_istream::get() works this way.
I would have prefered reading nothing, setting Line[0] to '\0', and
cin.gcount() == 0, without setting ios::failbit.
I'll bet people smarter than I debated it for months.
Can anyone explain why ios::failbit should be set in this case?
Here's updated code that works.
#include <iostream>
#include <limits>
using namespace std;
int main() {
static char Line[1024];
for (;;) {
if (!cin.get(Line, sizeof(Line))) {
if (cin.bad() || cin.eof())
break;
cin.clear();
}
cin.ignore(numeric_limits<int>::max(), '\n');
cout << ':' << Line << endl;
}
} // main
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]