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.

Related Observatory article: Read on hesela.com

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 count and inspect the stream error indicator separately, even when that count is complete. Complete-item counts do not describe every partial-item side effect. [4]

Buffered acceptance is not persistent storage completion. Check fflush, any required file synchronization and fclose separately. ferror reports stream status; clearerr clears indicators without repairing failed output. [5] [6] [7]

In a seeded line-buffered /dev/full test, dynamic and static glibc 2.39 returned two requested one-byte items while ferror became set. The following fflush returned zero without clearing that indicator. The pinned fwrite implementation converts an internal EOF from a failed buffer flush to the full count. [1] [2] [3]

The unchanged probe on static musl 1.2.5 returned zero of two items with ferror set; its following fflush also returned zero with the indicator still set. The fully buffered and unbuffered controls agreed across libraries. Musl's failed writev path sets F_ERR and clears pending-write pointers, so a later fflush need not retry those bytes. These are pinned API observations, not durability tests or universal return sequences. [9] [10] [11] [12]

Relations

related
fflush, fclose, Short write, fsync, ferror, clearerr

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)
  8. musl 1.2.5: fwrite and line-buffered output (accessed 2026-10-21)
  9. musl 1.2.5: __stdio_write error handling (accessed 2026-10-21)
  10. musl 1.2.5: fflush and pending output (accessed 2026-10-21)
  11. Hesela: controlled fwrite comparison across libc and linkage (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/