Georg Schölly
Georg Schölly

Reputation: 126095

What is the difference between read() and fread()?

I'm reading source code of the linux tool badblocks. They use the read() function there. Is there a difference to the standard C fread() function? (I'm not counting the arguments as a difference.)

Upvotes: 72

Views: 80655

Answers (7)

bad programmer
bad programmer

Reputation: 934

For Beginners like me in C/Systems programming domain. I am roughly quoting the answer from the lecture around this timestamp by Professor John Kubiatowicz.

fread is a high level C-API that internally uses the low-level read system call in an optimized way.

Imagine your system is optimized to read 4k bytes at a time. When you use fread to read from a file in a while loop, you will initiate read system call once to get a chunk of 4k bytes from the kernel and save it in user buffer. Now, all the subsequent reading for upto 4k bytes will happen from that user buffer. This is good because system calls are expensive.

This is also highlighted by the comment from @Joseph Garvin in his comment above.

Upvotes: 1

chakra t
chakra t

Reputation: 37

read() --> Directly using this system call to kernel and that performs the IO operation.

fread() --> Is a function provided in standard library.

Calling fread() is mainly used for binary file data where struct data are stored. 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.

Upvotes: 2

Joe
Joe

Reputation: 2976

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(). fread() will pull a block and dole it out as you ask for it. read() will drop to the kernel for each call.

Upvotes: 13

AIB
AIB

Reputation: 6064

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

Family read:

  • are system calls
  • are not formatted IO: we have a non formatted byte stream

Family fread

  • are functions of the standard C library (libc)
  • use an internal buffer
  • are formatted IO (with the "%.." parameter) for some of them
  • use always the Linux buffer cache

More details here, although note that this post contains some incorrect information.

Upvotes: 51

Darron
Darron

Reputation: 21628

read() is a low level, unbuffered read. It makes a direct system call on UNIX.

fread() is part of the C library, and provides buffered reads. It is usually implemented by calling read() in order to fill its buffer.

Upvotes: 76

Tor Klingberg
Tor Klingberg

Reputation: 5088

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.
  • read also blocks, but if you ask for say 4kB it may return after reading just 1kB, even if the file has not ended.

This can cause subtle bugs, as it depends on where the file is stored, caches, etc.

Upvotes: 5

phihag
phihag

Reputation: 287815

read is a syscall, whereas fread is a function in the C standard library.

Upvotes: 10

Related Questions