newbie questions - cout and stdout

G

Guest

The following expression:

cout << "hello" << "\n";

has two properties that confuse me.

First, on the system I'm on, text file lines are terminated by newline and
carriage return (both). Why does "\n" start a new line on my system? I
suppose I could ask the same question about printf() on comp.lang.c. Isn't
line termination system dependent?

Second, my book says, very clearly:

There are three events that cause the system to flush the buffer.
First, the buffer might be full ... Second, the library may be
asked to read from stdin stream ... Third, when we explicitly say
to do so.

None of the three conditions seem to exist in the expression above. Clearly,
the buffer is not less than 5 characters wide. Not even 5 wide characters!
;) I'm NOT asking to read from stdin. Lastly, I didnt' explicitly say to
flush the buffer.

So exactly why does the expression above seem to flush the stdout buffer?

Thanks!
Pete
 
V

Victor Bazarov

nospam said:
The following expression:

cout << "hello" << "\n";

has two properties that confuse me.

First, on the system I'm on, text file lines are terminated by newline and
carriage return (both). Why does "\n" start a new line on my system? I
suppose I could ask the same question about printf() on comp.lang.c. Isn't
line termination system dependent?

The single \n character when output is probably converted into \r\n (or
\n\r, pick one). That's taken care of the system library.
Second, my book says, very clearly:

There are three events that cause the system to flush the buffer.
First, the buffer might be full ... Second, the library may be
asked to read from stdin stream ... Third, when we explicitly say
to do so.

I think the #2 is only true when the input and output are synchronized.
None of the three conditions seem to exist in the expression above. Clearly,
the buffer is not less than 5 characters wide. Not even 5 wide characters!
;) I'm NOT asking to read from stdin. Lastly, I didnt' explicitly say to
flush the buffer.

So exactly why does the expression above seem to flush the stdout buffer?

Whatever your book says doesn't have to be the limited set. The book does
not say "There are _only_ three events that ...", does it? There can be
other events that flush the buffer. On your system, that is. Possibly
the output of \n can trigger flushing, right?

V
 
B

benben

Whatever your book says doesn't have to be the limited set. The book does
not say "There are _only_ three events that ...", does it? There can be
other events that flush the buffer. On your system, that is. Possibly
the output of \n can trigger flushing, right?

V

Or a flush call from destructor...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top