FREAD(3P)
β¨ FREAD(3P) β bestie edition β¨
okay so real talk, this is the POSIX Programmer's Manual page for fread, and I need you to clock the PROLOG tea first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. π
πΈ NAME πΈ
fread β binary input. that's it, that's the whole vibe. reading raw bytes, no thoughts, just data. π¦
π SYNOPSIS π
#include <stdio.h>
size_t fread(void *restrict ptr, size_t size, size_t nitems,
FILE *restrict stream);
periodt. two restrict qualifiers, and yes bestie, they're doing exactly what they sound like. π
π DESCRIPTION π
Okay so first things first, the functionality on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional, okay? Not on purpose! And this volume of POSIX.1β2017 defers to the ISO C standard. so like, ISO C is the referee here. π
Now the actual lore. The fread() function shall read into the array pointed to by ptr up to nitems elements whose size is specified by size in bytes, from the stream pointed to by stream. got it? size bytes, nitems of them, land them in ptr. simple.
For each object, size calls shall be made to the fgetc() function and the results stored, in the order read, in an array of unsigned char exactly overlaying the object. (not me imagining little byte-fairies lining up one at a time π it's literally one fgetc() per byte, no cap.)
The file position indicator for the stream (if defined) shall be advanced by the number of bytes successfully read. Important: if an error occurs, the resulting value of the file position indicator for the stream is unspecified. And if a partial element is read, its value is unspecified. we love an unspecified, we do not love relying on an unspecified. π
The fread() function may mark the last data access timestamp of the file associated with stream for update. The last data access timestamp shall be marked for update by the first successful execution of fgetc(), fgets(), fread(), fscanf(), getc(), getchar(), getdelim(), getline(), gets(), or scanf() using stream that returns data not supplied by a prior call to ungetc(). (yes, all nine of those get to do it, and yes, the ungetc() clause matters. iykyk.) β¨
π RETURN VALUE π
Upon successful completion, fread() shall return the number of elements successfully read β which is less than nitems only if a read error or end-of-file is encountered. those are the only two reasons, bestie, no other excuses. π
If size or nitems is 0, fread() shall return 0 and the contents of the array and the state of the stream remain unchanged. zero means zero, nothing moves, nothing changes, we simply vibe. π«Ά
Otherwise, if a read error occurs, the error indicator for the stream shall be set, and errno shall be set to indicate the error. so like, check your errno. π
π ERRORS π
Refer to fgetc(). that's the whole list, go look over there. πββοΈ
The following sections are informative. (so they're lore, not law. cute but not binding.) β¨
π EXAMPLES π
Reading from a Stream
okay example one, the glow-up: the following example transfers a single 100-byte fixed length record from the fp stream into the array pointed to by buf.
#include <stdio.h>
...
size_t elements_read;
char buf[100];
FILE *fp;
...
elements_read = fread(buf, sizeof(buf), 1, fp);
...
If a read error occurs, elements_read will be zero but the number of bytes read from the stream could be anything from zero to sizeof(buf)-1. so like, zero elements β zero bytes. do not get played. π
And example two, the other flavor: the following example reads multiple single-byte elements from the fp stream into the array pointed to by buf.
#include <stdio.h>
...
size_t bytes_read;
char buf[100];
FILE *fp;
...
bytes_read = fread(buf, 1, sizeof(buf), fp);
...
If a read error occurs, bytes_read will contain the number of bytes read from the stream. so here you actually get a count of what landed. slay for partial reads, honestly. β
π¦ APPLICATION USAGE π¦
The ferror() or feof() functions must be used to distinguish between an error condition and an end-of-file condition. like, you cannot tell them apart from fread()'s return value alone. go check. no skipping. π
Because of possible differences in element length and byte ordering, files written using fwrite() are application-dependent, and possibly cannot be read using fread() by a different application, or by the same application on a different processor. so like, your file is not universal, it's giving "this only works on my machine" energy. plan accordingly. π
π§ RATIONALE π§
None. nothing. empty. the void. π³οΈβ¨
π FUTURE DIRECTIONS π
None. no plans, no notes, we stay as we are. πΈ
π SEE ALSO π
- Section 2.5, Standard I/O Streams
feof()ferror()fgetc()fopen()fscanf()getc()gets()- The Base Definitions volume of
POSIX.1β2017,<stdio.h>
go read those, they're all in the group chat. π¬
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page bestie, brought to you by IEEE/The Open Group, 2017 βFREAD(3P), the POSIX Programmer's Manual, circa 2017. read responsibly, check yourferror(), and never trust a return value alone. mwah π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms