delorie.com/djgpp/faq/running/no-prompt.html   search  
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.

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.


  webmaster     delorie software   privacy  
  Copyright © 1995     Updated Feb 1995