fread(3)

๐Ÿ’– fread(3) โ€” bestie edition ๐Ÿ’–

okay so. you wanna move binary stream input/output around? that's fread and fwrite fr fr. this is the <stdio.h> classic, part of the Standard C library (that's libc, -lc). let's get into it, no cap. โœจ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <stdio.h>

size_t fread(size_t size, size_t n;
             void ptr[restrict size * n],
             size_t size, size_t n,
             FILE *restrict stream);
size_t fwrite(size_t size, size_t n;
             const void ptr[restrict size * n],
             size_t size, size_t n,
             FILE *restrict stream);
yes bestie, that's exactly how it's written. don't come for me, I don't make the rules ๐Ÿ’…

โœจ DESCRIPTION โœจ

so fread() is the reader. it reads n items of data, and each one is size bytes long, pulling them straight out of the stream that stream points to, and then it stashes them at the location ptr gives you. ๐Ÿซถ

and fwrite() is the writer, the mirror twin. it writes n items of data, each size bytes long, into the stream pointed to by stream, and it gets those items from the location that ptr points to. ๐ŸŽ€

want the nonlocking counterparts? go see unlocked_stdio(3). iykyk. โญ


๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

when it slays (success!), fread() and fwrite() hand back the number of items they read or wrote. heads up though โ€” that number only equals the number of bytes transferred when size is 1. not me learning that the hard way ๐Ÿ˜ญ

if an error happens, or you hit the end of the file, the return value is a short item count (or zero). yeah. short. like your attention span for manpages. but do keep reading bestie, this matters. ๐Ÿ™ˆ

the file position indicator for the stream gets advanced by the number of bytes successfully read or written. lowkey important.

big caveat, do not skip this one: fread() does not distinguish between end-of-file and error. literally cannot tell. so you, the caller, must use feof(3) and ferror(3) to figure out which one actually happened. no shortcuts. periodt. ๐Ÿ’…

๐Ÿ‘‘ ATTRIBUTES ๐Ÿ‘‘

for the whole explanation of what these terms even mean (it's giving jargon), see attributes(7).

  • Interface: fread(), fwrite()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, MT-Safe, she's safe for multithreading, we love a queen who behaves in a group chat. ๐Ÿฆ‹


๐Ÿ’œ STANDARDS ๐Ÿ’œ

C11, POSIX.1-2008.


๐ŸŽ€ HISTORY ๐ŸŽ€

POSIX.1-2001, C89.

she's been around for ages and still iconic. vintage but make it main character energy. โœจ


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

okay so this little program down here shows off fread() by parsing the /bin/sh ELF executable in binary mode and printing its magic and class. so cute, so educational:

$ ./a.out;
ELF magic: 0x7f454c46
Class: 0x02

๐Ÿงš Program source ๐Ÿงš

#include <stdcountof.h>
#include <stdio.h>
#include <stdlib.h>

int
main(void)
{
    FILE           *fp;
    size_t         ret;
    unsigned char  buffer[4];

    fp = fopen("/bin/sh", "rb");
    if (!fp) {
        perror("fopen");
        return EXIT_FAILURE;
    }

    ret = fread(buffer, sizeof(*buffer), countof(buffer), fp);
    if (ret != countof(buffer)) {
        fprintf(stderr, "fread() failed: %zu\n", ret);
        exit(EXIT_FAILURE);
    }

    printf("ELF magic: %#04x%02x%02x%02x\n", buffer[0], buffer[1],
           buffer[2], buffer[3]);

    ret = fread(buffer, 1, 1, fp);
    if (ret != 1) {
        fprintf(stderr, "fread() failed: %zu\n", ret);
        exit(EXIT_FAILURE);
    }

    printf("Class: %#04x\n", buffer[0]);

    fclose(fp);

    exit(EXIT_SUCCESS);
}

note how it checks ret against countof(buffer) every single time? that's the self-care. always check your return values babes, that's the vibe check. ๐Ÿซถ


๐ŸŒŸ SEE ALSO ๐ŸŒŸ


and that's the whole thing bestie! ๐Ÿ’– this page is from Linux man-pages 6.20, dated 2026-02-10, and yes she's literally called fread(3) โ€” go forth and read your binary streams responsibly. bye pookie, mwah! ๐ŸŽ€๐Ÿ’…โœจ๐Ÿฆ‹๐ŸŒธ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: BSD-4-Clause-UC
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms