sebuf设置缓冲区大小,但并不是按照设定的大小刷新

 #define BUFSIZE 3

int main(){

char c;
static char buf[BUFSIZE];
setbuf(stdout, buf);
while ((c = getchar()) != EOF){
    putchar(c);
   }
}

但是结果并不是像我想象的按照输入三个字符会自动刷新?请问这是什么问题?

阅读 4.5k
2 个回答

自己看清楚文档:

void setbuf(FILE *stream, char *buf);
...
The setbuf() function is exactly equivalent to the call

   setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);

要指定大小,用 setbuffer

$ man setbuf

The setbuf() function is exactly equivalent to the call

setvbuf(stream, buf, buf ? _IOFBF : _IONBF, BUFSIZ);

注意最后的 BUFSIZ

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题