将 C 程序设置为行缓冲区将不起作用 [英] Setting C program to line buffer won't work
问题描述
我试图强制我的 C 程序行缓冲(stdout 将被 java 程序捕获),但它似乎总是完全缓冲.下面是一些示例代码:
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);
}
如果我指定 _IOLBF 或 _IOFBF,那么在我输入字符之前我看不到输出.只有当我使用 _IONBF 时,我才会在 getchar() 之前看到输出._IOLBF 不应该做同样的事情,因为Hello World\n"包含一个 '\n'?
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'?
我使用的是 Visual C++ 2005.
I am using visual c++ 2005.
谢谢
推荐答案
根据 此 Microsoft 文档:
_IOLBF:对于某些系统,这提供了行缓冲.但是,对于 Win32,其行为与 _IOFBF - Full Buffering 相同.
_IOLBF: For some systems, this provides line buffering. However, for Win32, the behavior is the same as _IOFBF - Full Buffering.
这篇关于将 C 程序设置为行缓冲区将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!