liveBook · Manning

Fread

As explained in Modern C

fread is the C standard library function that reads a specified number of fixed-size items from a stream into memory.

The prototype `size_t fread(void* restrict ptr, size_t size, size_t nmemb, FILE* restrict stream);` reads `size * nmemb` bytes from the stream’s current file position into memory. A successful call advances that position; a return value smaller than the requested amount signals an error or incomplete operation. section 14.4

Use fread for binary data when these conditions matter:

  • Open the stream in binary mode so bytes are read without external-to-internal representation or end-of-line translation. section 14.4
  • Treat the stream as having a current file position; combine fread with fseek and ftell to move through a file. section 14.4
  • Check the return value rather than assuming that the complete requested amount was read. section 14.4 section 14.6
1   size_t fread(void* restrict ptr, size_t size, size_t nmemb,
2                FILE* restrict stream);
3   size_t fwrite(void const*restrict ptr, size_t size, size_t nmemb,
4                 FILE* restrict stream);
5   int fseek(FILE* stream, long int offset, int whence);
6   long int ftell(FILE* stream);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":11},{\"line\":0,\"ch\":16}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":21}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":22}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":1}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":1}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":2,\"ch\":11},{\"line\":2,\"ch\":17}],[{\"line\":2,\"ch\":18},{\"line\":2,\"ch\":28}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":10}],[{\"line\":2,\"ch\":4},{\"line\":2,\"ch\":10}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":1}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":0,\"ch\":23},{\"line\":0,\"ch\":31}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":31}],[{\"line\":2,\"ch\":29},{\"line\":2,\"ch\":37}],[{\"line\":3,\"ch\":24},{\"line\":3,\"ch\":32}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":1}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":12}],[{\"line\":4,\"ch\":8},{\"line\":4,\"ch\":13}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":36}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":12}],[{\"line\":4,\"ch\":4},{\"line\":4,\"ch\":7}],[{\"line\":5,\"ch\":9},{\"line\":5,\"ch\":12}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":1}],[{\"line\":4,\"ch\":28},{\"line\":4,\"ch\":36}],[{\"line\":5,\"ch\":4},{\"line\":5,\"ch\":12}],[{\"line\":5,\"ch\":13},{\"line\":5,\"ch\":18}],[{\"line\":1,\"ch\":17},{\"line\":1,\"ch\":21}],[{\"line\":3,\"ch\":18},{\"line\":3,\"ch\":22}],[{\"line\":4,\"ch\":14},{\"line\":4,\"ch\":18}],[{\"line\":5,\"ch\":19},{\"line\":5,\"ch\":23}]]"}
!@%STYLE%@!

Compare fread with fwrite, fseek, and ftell through their declarations and parameter types. section 14.4

How Modern C covers it

How deep it goes
Explains byte counts, file-position updates, binary-mode restrictions, and short returns, but does not develop implementation internals.
Examples
Uses the fread prototype and the artifact of storing and later reading a large object in its internal representation.
What it assumes
Assumes familiarity with streams, files, memory buffers, and C library return values.
What you can do after
Operate fread for binary-file input, advance through file positions, and detect incomplete reads through return values.
How it connects
Connects fread with binary streams, fwrite, fseek, ftell, file positions, and error handling.

Also explained in

Compare how 2 books explain fread →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.