From: "Ron Grunwald" Organization: Edith Cowan University To: jkeene AT unlinfo DOT unl DOT edu (Jon Keene) Date: Tue, 28 Feb 1995 12:58:11 GMT+800 Subject: Re: Question reagarding kbhit() Cc: djgpp AT sun DOT soe DOT clarkson DOT edu Jon found the following curious problem: > > Could anyone tell me why it is that when I run the following > program, it pauses for the keyboard hit _before_ doing any output? > > Ponderously yours, > Rumball > --- > > #include > #include > #define BUFSIZE 1840 /* (80 * 23) */ > #define BLANK ' ' > > main () { > > int i; > int buf[BUFSIZE]; > > for ( i = 0; i < BUFSIZE; i++ ) > buf[i] = BLANK; > buf[0] = 'a'; > buf[BUFSIZE - 1] = 'z'; > for ( i = 0; i < BUFSIZE; i++ ) > printf("%c", buf[i]); > while (!kbhit()) > ; > return 0; > } > What I think is happening here is that printf() buffers its output and only releases it at certain events, such as program termination. The problem can be resolved by replacing the following code, > for ( i = 0; i < BUFSIZE; i++ ) > printf("%c", buf[i]); > while (!kbhit()) with for ( i = 0; i < BUFSIZE; i++ ) { printf("%c", buf[i]); fflush(stdout); } while (!kbhit()) This forces printf()'s buffer to be written out to the file specified. In this case, the screen. Ron. ******************************************************************** | Author.............. Ron Grunwald | | Internet............ r DOT grunwald AT cowan DOT edu DOT au | | Phone............... (09)273 8027 or (09)273 8468 | |------------------------------------------------------------------| | Department.......... Computer Operations and Systems Management | | Division/Faculty.... Information Technology | | Institute........... Edith Cowan University, Churchlands | | Location............ Perth, Western Australia | ******************************************************************** "I don't have any solution but I certainly admire the problem!"