fwrite · shipping · also fwrite(), fwrite(3)

fwrite

fwrite is a C standard-I/O operation that attempts to write up to a requested number of fixed-size items to a stream and returns the number of complete items written.

The return value counts items, not bytes, unless size is one. For example, fwrite(data, 4, 3, stream) returns three on full success for twelve bytes. Compare against the requested item count and inspect the stream error indicator as a separate check, even when that count is complete. A complete-item count does not describe every possible partial-item side effect. [4]

Buffered stream acceptance is not persistent storage completion. Check subsequent fflush and, for a descriptor-backed stream requiring file synchronization, fsync; check fclose as well. ferror reports stream status, while clearerr clears indicators rather than repairing failed output. [5] [6] [7]

In a controlled glibc 2.39 test on Linux, a seeded line-buffered /dev/full stream returned two requested one-byte items for B plus newline while ferror became nonzero. A following fflush returned zero without clearing that indicator. The pinned implementation maps an internal EOF from a failed buffer flush to the full requested count; its file-write path sets the error indicator. This is version-specific behavior, not a universal libc guarantee or a durability test. The unbuffered control returned zero items; the fully buffered control reported its error at the later flush. Code, raw observations and scope are published separately. [1] [2] [3]

Relations

related
fflush, fclose, Short write, fsync

Limitations

  • Item counts must not be mistaken for byte counts.
  • Successful stream output alone does not establish file durability.
  • A full item count does not necessarily imply a clear stream error indicator.

Citations

  1. glibc 2.39: _IO_fwrite return-value handling (accessed 2026-10-21)
  2. glibc 2.39: newline flushing and stream errors (accessed 2026-10-21)
  3. Hesela: full fwrite counts and stream error indicators (accessed 2026-10-21)
  4. Linux man-pages 6.19: fread(3) and fwrite(3), return units (accessed 2026-10-21)
  5. Linux man-pages 6.19: fflush(3) (accessed 2026-10-21)
  6. Linux man-pages 6.19: ferror(3) and clearerr(3) (accessed 2026-10-21)
  7. Linux man-pages 6.19: fsync(2) (accessed 2026-10-21)

Further reading

  1. Hesela: The user-space buffer comes before fsync (accessed 2026-10-21)

JSON: /data/terms/fwrite.json

Published by Hesela team · Updated

Report a correction · Editorial standards · Reuse guidance

Cite this page
Hesela team. fwrite. Hesela. 2026-10-21. https://hesela.dev/terms/fwrite/