当前位置:  开发笔记 > 编程语言 > 正文

C相当于autoflush(每次写入后刷新stdout)?

如何解决《C相当于autoflush(每次写入后刷新stdout)?》经验,为你挑选了3个好方法。

在Perl中,我可以键入:

$|++;

打印到STDOUT的任何内容都将自动fflush()编辑.

C中有等价物吗?换句话说,有什么方法可以告诉stdio在每次printf()之后自动刷新stdout,它会自动刷新stderr吗?



1> Harry..:

试试setvbuf(stdout, NULL, _IONBF, 0).它将更改stdout为unbuffered(_IONBF)模式.



2> yogman..:

我没有这样做,但_IOLBF将是正确的答案.

$ man setvbuf
...
名称
setvbuf - 为流分配缓冲

大纲
#include

$ man setvbuf
NAME
       setvbuf — assign buffering to a stream

SYNOPSIS
       #include 

       int setvbuf(FILE *restrict stream, char *restrict buf, int type,
           size_t size);

DESCRIPTION
       The functionality described on this reference page is aligned with
       the ISO C standard. Any conflict between the requirements described
       here and the ISO C standard is unintentional. This volume of
       POSIX.1?2008 defers to the ISO C standard.

       The setvbuf() function may be used after the stream pointed to by
       stream is associated with an open file but before any other operation
       (other than an unsuccessful call to setvbuf()) is performed on the
       stream. The argument type determines how stream shall be buffered, as
       follows:

        *  {_IOFBF} shall cause input/output to be fully buffered.

        *  {_IOLBF} shall cause input/output to be line buffered.

        *  {_IONBF} shall cause input/output to be unbuffered.

描述
setvbuf()函数可以在stream指向的流与打开的文件关联之后但在对流执行任何其他操作(除了对setvbuf()的不成功调用之外)之前使用.参数类型确定如何缓冲流,如下所示:

$ man setvbuf
NAME
       setvbuf — assign buffering to a stream

SYNOPSIS
       #include 

       int setvbuf(FILE *restrict stream, char *restrict buf, int type,
           size_t size);

DESCRIPTION
       The functionality described on this reference page is aligned with
       the ISO C standard. Any conflict between the requirements described
       here and the ISO C standard is unintentional. This volume of
       POSIX.1?2008 defers to the ISO C standard.

       The setvbuf() function may be used after the stream pointed to by
       stream is associated with an open file but before any other operation
       (other than an unsuccessful call to setvbuf()) is performed on the
       stream. The argument type determines how stream shall be buffered, as
       follows:

        *  {_IOFBF} shall cause input/output to be fully buffered.

        *  {_IOLBF} shall cause input/output to be line buffered.

        *  {_IONBF} shall cause input/output to be unbuffered.



3> CesarB..:

看看setbuf()和setvbuf().

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有