开发者

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 fflush(stdout) nor setvbuf(stdout, NULL, _IONBF, 0) is working!!! Works great after disabling 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜