Buffering in the standard C API

The standard C API offers a family of functions that look just like the POSIX functions, but with the letter f on the front.

The fread(buffer, 1, count, fp) method reads raw bytes from the underlying file and stores them in the given buffer--up to count bytes, or until it encounters some sort of permanent error (for example, if someone unmounts the underlying filesystem in the middle of the read). It returns the number of bytes read and advances the cursor.

The literal 1 in that call is not a mistake! Technically, the function signature is fread(buffer, k, count, fp). It reads up to k * count bytes, or until it encounters a permanent error and returns the number of bytes read divided by k. However, in your ...

Get Mastering the C++17 STL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.