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.)
The fread() function in C++ reads the block of data from the stream. This function first, reads the count number of objects, each one with a size of size bytes from the given input stream.
The fread() function returns the number of full items successfully read, which can be less than count if an error occurs, or if the end-of-file is met before reaching count. If size or count is 0, the fread() function returns zero, and the contents of the array and the state of the stream remain unchanged.
The fread( ) function reads a specified number of bytes from a binary file and places them into memory at the specified location. The fwrite( ) function writes a specified number of bytes from the memory address specified and places them into the file.
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.
Family read()
-> open
, close
, read
, write
Family fread()
-> fopen
, fclose
, fread
, fwrite
More details here, although note that this post contains some incorrect information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With