Mail Archives: djgpp/1995/02/27/13:12:41
> Could anyone tell me why it is that when I run the following
> program, it pauses for the keyboard hit _before_ doing any output?
This is answered in the DJGPP FAQ list:
9.7 Q: My program prompts the user to enter data from the keyboard, then
reads its response. When compiled with a 16-bit compiler like
BCC or MSC it works as expected, but with gcc the prompt doesn't
show, or is printed much later in the program.
A: Do you write to screen using buffered I/O (fprintf(), fputs() and
the like) functions? Under most DOS-based compilers, the
standard streams stderr and stdout are buffered one line at a
time. This means that as soon as you write a Newline `\n' to
these streams, they are actually written to the screen. In
contrast, in DJGPP those streams are buffered with 4KB-long
buffer (the size of the transfer buffer), as any other open file
would. (That's because DJGPP tries to minimize the number of
switches to real mode required to actually write the buffered
characters.) The buffer is not written to screen until it's
full, which might produce very unpleasant and unexpected behavior
when used in interactive programs.
It is usually bad idea to use buffered I/O in interactive
programs; you should instead use screen-oriented functions like
cprintf() and cputs(). If you must use buffered I/O, you should
explicitly change the buffering of stdout and stderr by calling
setvbuf(); another solution would be to fflush() the output
stream before calling any input function, which will ensure all
pending output is written to the operating system. While this
will work under DOS and DJGPP, note that some operating systems
(including some DOS extenders) might further buffer your output,
so sometimes a call like sync() would be needed to actually cause
the output be delivered to the screen.
- Raw text -