Advertisement

cout.flush().....wtf?

Started by May 01, 2001 04:50 AM
0 comments, last by RageMatrix 23 years, 9 months ago
Hi, I''ve been learning winsock recently, and experimenting with a win32 console program just to see how all the bits of winsock work. However, early on, I realised that I needed some decent keyboard control routines like in a windowed app. Being a good little programmer, I dutifully looked on MSDN and found the ReadConsoleInput() function, which I used to detect the ESC key and break out from a loop. However, all my cout calls were not displayed until the key had been pressed. I was stumped by this, until a friendly person on #Gamedev told me to use cout.flush(), which flushes the output buffer to the screen (many thanks to the person who helped me, BTW). My question is, why is flushing the buffer necessary in this case, when before using ReadConsoleInput(), output was displayed straight away? RM "Peace Through Superior Firepower" -=Kicking Butt and Writing Code=-
The standard C++ IO libraries are based on ''streams''. A Stream is basically a buffer (imagine a toilet, seriously), in which you fill up with information a little at a time, and then flush all at once.

This allows most stream processing to occur independently from the output routines (streams are versatile enough to be outputted into anything, files, network, screen, custom classes, etc)


The reason you were not seeing any output is because you didn''t explicitly flush the cout buffer. Just putting newline characters into a stream will not flush it to screen, which Is why it is better to use endl instead of \n when dealing with streams. In the end, streams work like this because they allow more control.

===============================================
Have I no control, is my soul not mine?
Am I not just man, destiny defined?
Never to be ruled, nor held to heel!
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.

This topic is closed to new replies.

Advertisement