Setting C program to line buffer won't work

我的梦境 提交于 2019-12-11 07:36:32

问题


I'm trying to force my C program to line buffer (stdout is going to be captured by a java program), but it always seems to fully buffer instead. Here is some sample code:

#include <stdio.h>
#include <stdlib.h>

int main(){
    char c;
    setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
    printf("Hello world\n");
    c = getchar();
    printf("got char: %c\n", c);
}

If I specify _IOLBF or _IOFBF, then I don't see an output until I input a char. Only if I use _IONBF will I see output before the getchar(). Shouldn't _IOLBF do the same since "Hello World\n" contains a '\n'?

I am using visual c++ 2005.

Thanks


回答1:


According to this Microsoft documentation:

_IOLBF: For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.



来源:https://stackoverflow.com/questions/6653603/setting-c-program-to-line-buffer-wont-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!