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
- glibc 2.39: _IO_fwrite return-value handling
- glibc 2.39: newline flushing and stream errors
- Hesela: full fwrite counts and stream error indicators
- Linux man-pages 6.19: fread(3) and fwrite(3), return units
- Linux man-pages 6.19: fflush(3)
- Linux man-pages 6.19: ferror(3) and clearerr(3)
- Linux man-pages 6.19: fsync(2)