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 usefeof(3)andferror(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
- Copyright 1990-1991, The Regents of the University of California
- Copyright 2020, Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms