fputs和fflush,写入和缓冲过程 [英] fputs and fflush, writing and buffer process

查看:321
本文介绍了fputs和fflush,写入和缓冲过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在C语言中进行写入处理的方式感到困惑。因此,我有一个字符串s,我想将其写入输出。为此,我使用fputs:

I'm confused as to how the writing processing goes in C. So I have a string, s, that I want to write to the output. To do that, I use fputs:

fputs(s, stdout);

但是显然这不是写到输出中,而只是收集要写的数据吗?到底在哪里收集?因此,我必须等到程序退出或直到我调用fflush(),直到将输出实际写入stdout为止?我对吗?

But apparently this does not write to the output, but merely collect the data for writing? Where exactly is it collected? So I have to wait until the program exits or till I call fflush() till the output is actually written into stdout? Am I right?

推荐答案

C Standard IO流以以下三种模式之一运行:

The C Standard IO streams are operating in one of three modes:


  1. 完全缓冲

  2. 行缓冲

  3. 未缓冲

您可以使用 setvbuf()函数。这一切都发生在Standard IO实现的内在深处。如果要立即显示数据,请使用非缓冲模式。

You can set the mode with the setvbuf() function. This all happens deep in the guts of the Standard IO implementation. If you want your data to appear immediately, use unbuffered mode.

从C99 7.19.3#3引用:

Quoting from C99 7.19.3#3:

当流是 unbuffered 时,字符应尽快从
源或目标位置出现。否则,字符可能是
的累积块,并以块的形式传输到主机环境或从主机环境传输出去。当
流被完全缓冲时,打算在填充缓冲区时将字符作为块发送到主机环境
或从主机环境发送
。当流被 line
缓冲
时,字符将在遇到换行符时作为块与主机
环境进行传输。此外,
字符打算在填充缓冲区时,在无缓冲流上请求输入时作为块传输到主机环境
,或者在行上请求输入时作为
块传输到主机环境需要主机环境传输
个字符的缓冲流。对这些特性的支持是
实现定义的,可能会受到 setbuf setvbuf 函数的影响。

When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via thesetbufandsetvbuf functions.

这篇关于fputs和fflush,写入和缓冲过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆