Flushing output buffer in C (cgi)
The following code:
int z = 0;
while(z < 4)
{
printf("iteration %d\n",z);
sleep(1);
z++;
}开发者_开发技巧
Works fine and stdout buffer is flushed every second if running the program from command line. However, when I try to access the program in a web browser (server - apache on linux, compiled executable (with gcc) handled through cgi), the content is displayed only after 4 seconds and not "step by step". I am looking for something like PHP's ob_flush()
. And, by the way, is cgi the best way of processing compiled C executables?
Update: neither Works great after disabling fflush(stdout)
nor setvbuf(stdout, NULL, _IONBF, 0)
is working!!!mod_deflate
.
I am not quite sure I understand your question correctly, but in C you can
- Flush after each print (
fflush
) Disable buffering (
setbuf
,setvbuf
)setvbuf(stdout, NULL, _IONBF, 0); /* this will disable buffering for stdout */
If these won't work, then either something else is doing buffering or buffering is not the problem.
You could try to fflush
stdout
after your printf
.
精彩评论