简体   繁体   English

read() 和 fread() 有什么区别?

[英]What is the difference between read() and fread()?

I'm reading source code of the linux tool badblocks .我正在阅读 linux 工具badblocks的源代码。 They use the read() function there.他们在那里使用read() function。 Is there a difference to the standard C fread() function?与标准 C fread() function 有区别吗? (I'm not counting the arguments as a difference.) (我没有将 arguments 视为差异。)

read() is a low level, unbuffered read. read()是低级别的无缓冲读取。 It makes a direct system call on UNIX.它对 UNIX 进行直接系统调用。

fread() is part of the C library, and provides buffered reads. fread()是 C 库的一部分,并提供缓冲读取。 It is usually implemented by calling read() in order to fill its buffer.它通常通过调用read()来填充其缓冲区来实现。

Family read() -> open , close , read , write家庭read() -> open , close , read , write
Family fread() -> fopen , fclose , fread , fwrite家庭fread() -> fopenfclosefreadfwrite

Family read:家人朗读:

  • are system calls是系统调用
  • are not formatted IO: we have a non formatted byte stream未格式化 IO:我们有一个未格式化的字节 stream

Family fread家庭恐惧症

  • are functions of the standard C library (libc)是标准 C 库 (libc) 的函数
  • use an internal buffer使用内部缓冲区
  • are formatted IO (with the "%.." parameter) for some of them其中一些被格式化为 IO(带有“%..”参数)
  • use always the Linux buffer cache始终使用 Linux 缓冲区缓存

More details here , although note that this post contains some incorrect information.更多细节在这里,虽然请注意这篇文章包含一些不正确的信息。

As I remember it the read() level APIs do not do buffering - so if you read() 1 byte at a time you will have a huge perf penalty compared to doing the same thing with fread() .我记得read()级别的 API 不做缓冲 - 所以如果你read()一次 1 个字节,与使用fread()做同样的事情相比,你将受到巨大的性能损失。 fread() will pull a block and dole it out as you ask for it. fread()将拉出一个块并根据您的要求将其分发出去。 read() will drop to the kernel for each call. read()将下降到 kernel 对于每个调用。

read is a syscall, whereas fread is a function in the C standard library. read是一个系统调用,而fread是 C 标准库中的 function。

One difference you should be aware of if you are converting code that uses one to using the other:如果您将使用一种的代码转换为使用另一种的代码,您应该注意一个区别:

  • fread blocks until the number of bytes you asked for has been read, or the file ends, or an error occurs. fread阻塞,直到您请求的字节数被读取,或文件结束,或发生错误。
  • read also blocks, but if you ask for say 4kB it may return after reading just 1kB, even if the file has not ended. read也块,但如果您要求说 4kB,它可能会在仅读取 1kB 后返回,即使文件尚未结束。

This can cause subtle bugs, as it depends on where the file is stored, caches, etc.这可能会导致细微的错误,因为它取决于文件的存储位置、缓存等。

read() --> Directly using this system call to kernel and that performs the IO operation. read() --> 直接使用此系统调用 kernel 并执行 IO 操作。

fread() --> Is a function provided in standard library. fread() --> 是标准库中提供的 function。

Calling fread() is mainly used for binary file data where struct data are stored.调用fread()主要用于存储结构数据的二进制文件数据。 The main difference between these two is the number of system calls in your application.这两者之间的主要区别在于应用程序中系统调用的数量。

The fread() kind of standard IO library functions are optimized for system calls, rather your application making system calls. fread()类型的标准 IO 库函数针对系统调用进行了优化,而不是您的应用程序进行系统调用。

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

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