Re: VS2005 C++ Express basic_istream::get(buf, size, delim) bug
Terry G <tjgolubi@netins.net> wrote:
Is this a compiler bug? Does your C++ compiler produce the same
results? terry
=========================================================
File: try_get.cpp
=========================================================
#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
=========================================================
Compiling: cl -EHsc try_get.cpp
Running: try_get < try_get.cpp
yields:
=========================================================
:#include <iostream>
:#include <limits>
=========================================================
Compiling: g++ try_get.cpp
Running: a < try_get.cpp
Yields:
=========================================================
:#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
g++ is wrong. 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.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925