jon@cit-vax.Caltech.Edu (Jonathan P. Leech) (03/25/86)
Summary:
Expires:
Sender:
Followup-To:
Organization : California Institute of Technology
Keywords:
The C++ book claims (sec 8.4.4; page 240) that
"...cin is tied to cout; this means that cin executes a
cout.flush(); // write output buffer
before attempting to read characters from its file."
Playing with various forms of stream I/O leaves me puzzled about
this assertion. Consider the following example:
__@/ cat junk.c
#include <stream.h>
main()
{
char buf[120];
// Test buffering of get(char *, int)
// Should read and echo two lines
for (int i = 0; i < 2; i++) {
cin.get(buf, 120);
cout << "get(char *, int): " << buf << "\n";
}
// Test buffering of get(char &)
// Should also read and echo two lines
for (i = 0; i < 2; i++) {
int j = 0;
while (!cin.eof()) {
cin.get(buf[j++]);
if (buf[j-1] == '\n')
break;
}
buf[j - 1 + cin.eof()] = '\0';
cout << "get(char &): " << buf << "\n";
}
}
__@/ junk
This is line 1 < I type this
This is line 2 < and this
This is line 3 < and this (no EOF)
get(char *, int): This is line 1 < THEN, it prints out these lines
get(char *, int):
get(char &):
get(char &): This is line 2
I expected something more like
This is line 1
get(char *, int): This is line 1
This is line 2
get(char *, int): This is line 2
This is line 3
get(char &): This is line 3
This is line 4
get(char &): This is line 4
Can anyone tell me why cout is not buffered as the book seems to
claim it should be, or why the middle two read operations got nothing
at all? I don't understand this at all.
Thanks,
Jon Leech (jon@csvax.caltech.edu || ...seismo!cit-vax!jon)
__@/