1FREAD(3)                   Linux Programmer's Manual                  FREAD(3)
2
3
4

NAME

6       fread, fwrite - binary stream input/output
7

SYNOPSIS

9       #include <stdio.h>
10
11       size_t fread(void *restrict ptr, size_t size, size_t nmemb,
12                    FILE *restrict stream);
13       size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb,
14                    FILE *restrict stream);
15

DESCRIPTION

17       The  function  fread() reads nmemb items of data, each size bytes long,
18       from the stream pointed to by stream,  storing  them  at  the  location
19       given by ptr.
20
21       The function fwrite() writes nmemb items of data, each size bytes long,
22       to the stream pointed to by stream, obtaining them  from  the  location
23       given by ptr.
24
25       For nonlocking counterparts, see unlocked_stdio(3).
26

RETURN VALUE

28       On  success,  fread()  and  fwrite() return the number of items read or
29       written.  This number equals the number of bytes transferred only  when
30       size  is 1.  If an error occurs, or the end of the file is reached, the
31       return value is a short item count (or zero).
32
33       The file position indicator for the stream is advanced by the number of
34       bytes successfully read or written.
35
36       fread() does not distinguish between end-of-file and error, and callers
37       must use feof(3) and ferror(3) to determine which occurred.
38

ATTRIBUTES

40       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
41       tributes(7).
42
43       ┌────────────────────────────────────────────┬───────────────┬─────────┐
44Interface                                   Attribute     Value   
45       ├────────────────────────────────────────────┼───────────────┼─────────┤
46fread(), fwrite()                           │ Thread safety │ MT-Safe │
47       └────────────────────────────────────────────┴───────────────┴─────────┘
48

CONFORMING TO

50       POSIX.1-2001, POSIX.1-2008, C89.
51

EXAMPLES

53       The  program  below  demonstrates the use of fread() by parsing /bin/sh
54       ELF executable in binary mode and printing its magic and class:
55
56           $ ./a.out
57           ELF magic: 0x7f454c46
58           Class: 0x02
59
60   Program source
61
62       #include <stdio.h>
63       #include <stdlib.h>
64
65       #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
66
67       int
68       main(void)
69       {
70           FILE *fp = fopen("/bin/sh", "rb");
71           if (!fp) {
72               perror("fopen");
73               return EXIT_FAILURE;
74           }
75
76           unsigned char buffer[4];
77
78           size_t ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fp);
79           if (ret != ARRAY_SIZE(buffer)) {
80               fprintf(stderr, "fread() failed: %zu\n", ret);
81               exit(EXIT_FAILURE);
82           }
83
84           printf("ELF magic: %#04x%02x%02x%02x\n", buffer[0], buffer[1],
85                  buffer[2], buffer[3]);
86
87           ret = fread(buffer, 1, 1, fp);
88           if (ret != 1) {
89               fprintf(stderr, "fread() failed: %zu\n", ret);
90               exit(EXIT_FAILURE);
91           }
92
93           printf("Class: %#04x\n", buffer[0]);
94
95           fclose(fp);
96
97           exit(EXIT_SUCCESS);
98       }
99

SEE ALSO

101       read(2), write(2), feof(3), ferror(3), unlocked_stdio(3)
102

COLOPHON

104       This page is part of release 5.12 of the Linux  man-pages  project.   A
105       description  of  the project, information about reporting bugs, and the
106       latest    version    of    this    page,    can     be     found     at
107       https://www.kernel.org/doc/man-pages/.
108
109
110
111GNU                               2021-03-22                          FREAD(3)
Impressum