What's the difference between read and fread?
fread
is a standardized C programming language function that works with a FILE
pointer.
read
is a C function available on a POSIX compatible system that works with a file descriptor.
how do read() and fread() work?
fread
calls some unknown system defined API to read from a file. To know how specific fread
works you have to look at specific implmeentation of fread
for your specific system - windows fread
is very different from Linux fread
. Here's implementation of fread
as part of glibc library https://github.com/lattera/glibc/blob/master/libio/iofread.c#L30 .
read()
calls read system call. https://man7.org/linux/man-pages/man2/syscalls.2.html
what do unbuffered read and buffered read mean?
fread
reads data to some intermediate buffer, then copies from that buffer to the memory you passed as argument.
read
makes kernel to copy straight to the buffer you passed as argument. There is no intermediate buffer.
why does some say fread() is faster than read() in this topic?
The assumption is that calling system calls is costly - what kernel does inside the system call and system call itself is costly.
Reading the data in a big like 4 kilobyte chunk into an intermediate buffer once and then reading from that buffer in small chunks within your program is faster than context switching to kernel every time to read a small chunks of data so that kernel will do repeatedly a small input/output operation to fetch the data.