liveBook · Manning

Fwrite

As explained in Modern C

fwrite is the C standard library function that writes a specified number of fixed-size items from memory to a stream.

fwrite writes size*nmemb bytes from memory to the current position of a stream. Its return value is the number of bytes written; a value below size*nmemb signals an error. section 14.4

Writing binary data

Open the stream in binary mode, call fwrite with a memory pointer, item size, item count, and stream, then check the returned byte count. A successful call advances the stream's file position by the bytes written. section 14.4

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%@!

The related declarations show fwrite alongside fread, fseek, and ftell, clarifying its parameters and its role in positioned binary I/O. section 14.4

Use fwrite with these constraints:

  • Use binary-mode streams for portable binary I/O; text-mode translation can alter a file's internal structure. section 14.4
  • Combine fwrite with fseek and ftell to move through a file and write at selected positions. section 14.4
  • Treat binary files as platform dependent, especially when preserving an object's internal representation. section 14.6

How Modern C covers it

How deep it goes
Explains parameters, byte counts, position updates, binary mode, and short-write errors, but defers broader error-case analysis.
Examples
Uses binary files, byte-preserving object storage, and paired fread/fwrite operations as concrete applications.
What it assumes
Assumes familiarity with C streams and memory pointers while emphasizing binary mode and byte-count checking.
What you can do after
Operate binary-file output, check short writes, and reposition file access with related stream-position functions.
How it connects
Connects fwrite with binary streams, fread, fseek, ftell, file positions, and platform-dependent files.

Also explained in

Compare how 2 books explain fwrite →

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.