{
  "slug": "fwrite",
  "term": "fwrite",
  "aliases": [
    "fwrite()",
    "fwrite(3)"
  ],
  "definition": "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.",
  "note": "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. [linux-fwrite]\n\nBuffered 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. [linux-fflush] [linux-ferror] [linux-fsync]\n\nIn 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. [glibc-fwrite] [glibc-fileops] [hesela-fwrite-error]\n\nThe 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. [musl-fwrite] [musl-write] [musl-fflush] [hesela-cross-libc]",
  "status": "shipping",
  "advantages": [],
  "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."
  ],
  "related": [
    "fflush",
    "fclose",
    "short-write",
    "fsync",
    "ferror",
    "clearerr"
  ],
  "supersedes": [],
  "supersededBy": [],
  "firstSeen": null,
  "lastSeen": null,
  "citations": [
    {
      "id": "glibc-fwrite",
      "title": "glibc 2.39: _IO_fwrite return-value handling",
      "url": "https://raw.githubusercontent.com/bminor/glibc/glibc-2.39/libio/iofwrite.c",
      "accessed": "2026-10-21"
    },
    {
      "id": "glibc-fileops",
      "title": "glibc 2.39: newline flushing and stream errors",
      "url": "https://raw.githubusercontent.com/bminor/glibc/glibc-2.39/libio/fileops.c",
      "accessed": "2026-10-21"
    },
    {
      "id": "hesela-fwrite-error",
      "title": "Hesela: full fwrite counts and stream error indicators",
      "url": "https://hesela.com/analyses/fsync-failure-retry-durability/#fwrite-indicator",
      "accessed": "2026-10-21"
    },
    {
      "id": "linux-fwrite",
      "title": "Linux man-pages 6.19: fread(3) and fwrite(3), return units",
      "url": "https://man7.org/linux/man-pages/man3/fwrite.3.html",
      "accessed": "2026-10-21"
    },
    {
      "id": "linux-fflush",
      "title": "Linux man-pages 6.19: fflush(3)",
      "url": "https://man7.org/linux/man-pages/man3/fflush.3.html",
      "accessed": "2026-10-21"
    },
    {
      "id": "linux-ferror",
      "title": "Linux man-pages 6.19: ferror(3) and clearerr(3)",
      "url": "https://man7.org/linux/man-pages/man3/ferror.3.html",
      "accessed": "2026-10-21"
    },
    {
      "id": "linux-fsync",
      "title": "Linux man-pages 6.19: fsync(2)",
      "url": "https://man7.org/linux/man-pages/man2/fsync.2.html",
      "accessed": "2026-10-21"
    },
    {
      "id": "hesela-stdio",
      "title": "Hesela: The user-space buffer comes before fsync",
      "url": "https://hesela.com/analyses/fsync-failure-retry-durability/#stdio-buffer",
      "accessed": "2026-10-21"
    },
    {
      "id": "musl-fwrite",
      "title": "musl 1.2.5: fwrite and line-buffered output",
      "url": "https://git.musl-libc.org/cgit/musl/tree/src/stdio/fwrite.c?h=v1.2.5",
      "accessed": "2026-10-21"
    },
    {
      "id": "musl-write",
      "title": "musl 1.2.5: __stdio_write error handling",
      "url": "https://git.musl-libc.org/cgit/musl/tree/src/stdio/__stdio_write.c?h=v1.2.5",
      "accessed": "2026-10-21"
    },
    {
      "id": "musl-fflush",
      "title": "musl 1.2.5: fflush and pending output",
      "url": "https://git.musl-libc.org/cgit/musl/tree/src/stdio/fflush.c?h=v1.2.5",
      "accessed": "2026-10-21"
    },
    {
      "id": "hesela-cross-libc",
      "title": "Hesela: controlled fwrite comparison across libc and linkage",
      "url": "https://hesela.com/analyses/fsync-failure-retry-durability/#fwrite-libc",
      "accessed": "2026-10-21"
    }
  ],
  "updated": "2026-10-21"
}
