简体   繁体   English

fwrite缓冲输出吗?

[英]Does fwrite buffer the output?

In C++, there is an std::fwrite() which writes a buffer to a file on disk. 在C ++中,有一个std::fwrite() ,它将缓冲区写入磁盘上的文件。

Can you please tell me if there is any buffer inside that fwrite implementation? 如果fwrite实现中有任何缓冲区,你能告诉我吗?

ie if I call fwrite() multiple times (say 10 times), does it actually invoke file I/O 10 different times? 即如果我多次调用fwrite()(比如说10次),它实际上是否会调用10次不同的文件I / O?

I am asking this for Ubuntu 10.04 environment. 我问Ubuntu 10.04环境。

Yes, it is buffered. 是的,它是缓冲的。 The size of the buffer is defined by BUFSIZ . 缓冲区的大小由BUFSIZ定义。 (Thanks @ladenedge.) Ten sufficiently large operations will result in ten system calls. (谢谢@ladenedge。)十个足够大的操作将导致十次系统调用。

You are also allowed to set your own buffer using std::setbuf and std::setvbuf . 您还可以使用std::setbufstd::setvbuf设置自己的缓冲区。

The functions in cstdio are inherited from C. The preferred interface in C++ is fstream and filebuf . cstdio中的函数继承自C语言.C ++中的首选接口是fstreamfilebuf

It depends. 这取决于。 On most platforms, file IO is cached hence the fflush() call exists to write cached data to disk - in that respect, the answer to your first question is (usually) "yes", and your second "no". 在大多数平台上,文件IO被缓存,因此存在将缓存数据写入磁盘的fflush()调用 - 在这方面,您的第一个问题的答案是(通常)“是”,而您的第二个问题是“否”。 That said, it's not guaranteed to be that way by any stretch of the imagination for any particular platform - generally, the standards only specify the interface for such functions, not their implementation. 也就是说,任何特定平台的任何想象力都不能保证这种方式 - 通常,标准只规定了这些功能的接口,而不是它们的实现。 Also, its quite possible that calling fwrite() will cause an implicit flush if the cache becomes "full", in which case calling fwrite() may in fact trigger file IO - especially if calling fwrite() with large amounts of data. 此外,如果缓存变为“满”,调用fwrite()很可能会导致隐式刷新,在这种情况下,调用fwrite()实际上可能会触发文件IO - 尤其是在调用具有大量数据的fwrite()时。

FILE* I/O can be buffered but it does not have to be (it can be different for each stream). FILE* I / O可以缓冲,但不一定是(每个流可以不同)。 Furthermore, there are multiple ways to do buffering (fully buffered where the buffer is not flushed until it is full or an explicit call to fflush is made or line buffered where the buffer is not flushed until it sees an EOL). 此外,有多种方法可以进行缓冲(完全缓冲,其中缓冲区未被刷新,直到它已满或显式调用fflush或行缓冲,其中缓冲区未被刷新,直到它看到EOL)。 It is also possible to turn off buffering altogether. 也可以完全关闭缓冲。

You can change the type of buffering with the setvbuf call. 您可以使用setvbuf调用更改缓冲类型。

It depends on the library implementation. 这取决于库的实现。 You might take a look at something like dtruss or strace to see what system calls are actually invoked by the implementation. 您可以查看dtrussstrace之类的内容,以查看实现实际调用的系统调用。

Why do you care about this? 你为什么关心这个?

I don't believe the standards says anything about this but in general: no the operating system will decide how many I/O operations to do. 我不相信标准说明了这一点,但总的来说:操作系统不会决定要做多少I / O操作。 It could be 1 it could be 10. It could even be more if the data size is large. 如果数据大小很大,它可能是1甚至可能是10。 Most operating systems allow you to control how I/o should behave but AFAIK there's no portable way to do it. 大多数操作系统允许您控制I / o的行为方式,但AFAIK没有可移植的方法。 (and often you don't want to anyway) (通常你不想要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM