Re: Visual C++ 2005 basic_istream::get(buf, size, delim) bug?
denise.kleingeist@googlemail.com wrote:
kanze wrote:
denise.kleingeist@googlemail.com wrote:
Terry G wrote:
int main() {
static char Line[1024];
while (std::cin.get(Line, sizeof(Line), '\n')) {
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
Note that the above line will always extract at most one
character! Only if the call to get() already hit EOF without
failing (e.g. because the last line is not properly terminated
by an end of line character) the above line might extract a
different number of character than one: in this case it would
extract no characters. See 27.6.1.3
(lib.istream.unformatted)/8-9 and /24-25 for details.
What happens if the program encounters a line of more than 1023
characters?
You are right: in this case the ignore might skip over more
than one character. I'd use std::string with std::getline()
anyway.
That's generally what I'd do as well. On the other hand, my
programs don't read data from possibly hostile "foreign"
sources; I could imagine something like Terry's code being used
to explicitly limit resource use, for example, to prevent a DoS
attack.
Concerning the mode of std::cin, the C++ standard is rather
vague and doesn't really say anything (27.3.1/1): "The object
cin controls input from a stream buffer associated with the
object stdin, declared in <cstdio>." The doesn't mean anything
by itself. QoI, of course, sets some expectations.
Actually, it means quite a bit. The C standard requires that
stdin be open in text mode. (?7.19.1/7: "At program startup,
three *text* [emphesis added] streams are predefined and need
not be opened explicitly[...]".
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]