On this page
fread
Prev: fclose | Up: stdio.h | Next: fwrite
Rel: stdio.h, stdio.h & File I/O Guide
Ref: cppreference — fread
Tags: #public #prog #c #stdlib #stdio #tier/core
Include
#include <stdio.h>
Signature & Overview
size_t fread(void *restrict buf, size_t size, size_t count, FILE *restrict stream);
Reads up to count objects of size bytes from stream; returns the number of full objects read.
Notes & Pitfalls
- Returns the number of elements read, not bytes: Pass
fread(buf, 1, sizeof(buf), f)so the return value is exact bytes read.
Example
#include <assert.h>
#include <stdio.h>
int main(void) {
FILE *f = tmpfile();
fwrite("ABCD", 1, 4, f);
rewind(f);
char out[4];
assert(fread(out, 1, 4, f) == 4 && out[0] == 'A');
fclose(f);
}
Linked from
Related notes
- tuts/c/stdlib/stdio/fclose Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fwrite Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio Linked here; links to this page; shared tags: #c, #prog, #stdio, #stdlib
- tuts/c/stdlib/stdio_and_file_io Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/stdio/feof shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/ferror shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fflush shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fgets shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fopen shared tags: #c, #prog, #stdio, #stdlib, #tier
- tuts/c/stdlib/stdio/fprintf shared tags: #c, #prog, #stdio, #stdlib, #tier