简体   繁体   English

fscanf和fprintf是否在C中缓冲?

[英]Are fscanf and fprintf buffered in C?

I wish to write an efficient C program, that makes a copy of a file. 我希望编写一个高效的C程序,该程序可以复制文件。 There doesn't seem to be a function (such as rename ) that performs this. 似乎没有执行此操作的功能(例如named )。 I plan to use fscanf an fprintf in stdio.h, but their descriptions do not say how or if they are buffered. 我计划在stdio.h中使用fscanffprintf ,但它们的描述未说明如何或是否对其进行缓冲。 Are they buffered between different cache levels? 它们是否在不同的缓存级别之间缓冲? (I assume disk to memory buffer is handled by the OS) (我假设磁盘到内存缓冲区是由操作系统处理的)

When you open a file with fopen , it will be fully buffered. 当您使用fopen打开文件时,该文件将被完全缓冲。

You can change the buffering before doing any other operations on the file, using setvbuf ( reference ). 您可以使用setvbufreference )在对该文件执行任何其他操作之前更改缓冲。

Using any normal I/O functions on a FILE object will take advantage of the buffering. FILE对象上使用任何普通的I / O功能都将利用缓冲。

If you are just copying the data, you will be doing sequential reads and writes, and will not necessarily need buffering. 如果仅复制数据,则将进行顺序读取和写入,并且不一定需要缓冲。 But doing that efficiently does require choosing an appropriate block size for I/O operations. 但是有效地做到这一点确实需要为I / O操作选择适当的块大小。 Traditionally, this is related to the size of a disk sector (4096 bytes) but that value isn't future-proof. 传统上,这与磁盘扇区的大小(4096字节)有关,但是该值不能用于将来。 The default used by fopen is BUFSIZ . fopen使用的默认值为BUFSIZ

As with any optimisation, construct actual tests to verify your performance gains (or losses). 与任何优化一样,构造实际测试以验证您的性能提升(或损失)。

In the end, for the fastest I/O you might have to use OS-specific APIs. 最后,为了获得最快的I / O,您可能必须使用特定于操作系统的API。 The CI/O functions just map to the general case of those APIs, but there may be special performance settings for an OS that you cannot control through the C library. CI / O功能仅映射到这些API的一般情况,但是对于无法通过C库控制的OS,可能会有特殊的性能设置。 I certainly ran into this when writing a fast AVI writer for Windows. 在为Windows编写快速AVI编写器时,我当然遇到了这个问题。 Using platform-specific I/OI was able to achieve the maximum read/write speed of the disk: twice the speed of buffered I/O ( <stdio.h> ) or the native AVI API, and about 20% faster than traditional C unbuffered I/O. 使用特定于平台的I / OI可以达到磁盘的最大读写速度:缓冲I / O( <stdio.h> )或本地AVI API的速度的两倍,并且比传统C语言快约20%无缓冲的I / O。

The printf and scanf family of functions are all part of the same buffered "interface". printfscanf系列功能都是同一缓冲“接口”的一部分。 man 3 stdio : man 3 stdio

The standard I/O library provides a simple and efficient buffered stream I/O interface. 标准I / O库提供了一个简单而有效的缓冲流I / O接口。 Input and output is mapped into logical data streams and the physical I/O characteristics are concealed. 输入和输出被映射到逻辑数据流,并且物理I / O特性被隐藏。 The functions and macros are listed below; 函数和宏在下面列出; more information is available from the individual man pages. 有关更多信息,请参见各个手册页。

If you want to avoid buffering, you will have to use a different C library. 如果要避免缓冲,则必须使用其他C库。

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

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