Mail Archives: djgpp/1998/09/21/19:30:26
Uhfgood wrote:
>
> Well here's is the simple question. consider the following code :
>
> printf("This demonstrates a \\a escape sequence: \a");
> getch();
>
> Now answer me this, how come it waits until a key is pressed to display the
> string?
>
> Now please I already know two ways to make it work like I think it's supposed
> to.
> one is to put a \n after the last part of the screen, and the other is to type
> "fflush(stdout);" but that still doesn't answer my basic question. Please just
> explain it to me? Also if you don't want to bother with this message after you
> read it just close it, instead of wasting your time writing me back a flame.
> (I'm not trying to be mean about, I just mean I figure it would take more
> effort to flame me than it would to either ignore the question, or tell me the
> answer)..
Firstly, did you read the FAQ?
The reason is buffering. In DJGPP, file I/O requires a call to DOS,
which requires a switch to real mode. All this is rather slow.
Therefore, I/O is buffered, which means the library accumulates a larger
chunk of data before writing it, which decreases this overhead.
The `stdout' stream by default uses line buffering, which means the data
is not written until a newline character ('\n') is output. This yields
a large performance advantage especially in cases where data is being
written one character at a time (with `putc', for instance). Thus, when
you print a newline, the data is actually written to the device (the
console, in this case).
`fflush' is provided to get around this buffering; it has the effect of
immediately flushing the buffer of that stream and writing its data
immediately.
With `setvbuf', you can change the buffering parameters of streams.
--
Nate Eldredge
nate AT cartsys DOT com
- Raw text -