Consider the following line of code:
while((n = read(STDIN_FILENO, buff, BUFSIZ)) > 0)
As per my understanding read/write
f
As the read()
manpage states:
Return Value
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.
So, each read()
will read up to the number of specified bytes; but it may read less. "Non-buffered" means that if you specify read(fd, bar, 1)
, read will only read one byte. Buffered IO attempts to read in quanta of BUFSIZ
, even if you only want one character. This may sound wasteful, but it avoids the overhead of making system calls, which makes it fast.