Debian Codegraph @

upstream/debian-trixie/man3/fread.3

raw
1 '\" t
2 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
3 .\" and Copyright (c) 2020 Arkadiusz Drabczyk <[email protected]>
4 .\" All rights reserved.
5 .\"
6 .\" This code is derived from software contributed to Berkeley by
7 .\" Chris Torek and the American National Standards Committee X3,
8 .\" on Information Processing Systems.
9 .\"
10 .\" SPDX-License-Identifier: BSD-4-Clause-UC
11 .\"
12 .\" @(#)fread.3 6.6 (Berkeley) 6/29/91
13 .\"
14 .\" Converted for Linux, Mon Nov 29 15:37:33 1993, [email protected]
15 .\" Sun Feb 19 21:26:54 1995 by faith, return values
16 .\" Modified Thu Apr 20 20:43:53 1995 by Jim Van Zandt <[email protected]>
17 .\" Modified Fri May 17 10:21:51 1996 by Martin Schulze <[email protected]>
18 .\"
19 .TH fread 3 2024-06-15 "Linux man-pages 6.9.1"
20 .SH NAME
21 fread, fwrite \- binary stream input/output
22 .SH LIBRARY
23 Standard C library
24 .RI ( libc ", " \-lc )
25 .SH SYNOPSIS
26 .nf
27 .B #include <stdio.h>
28 .P
29 .BI "size_t fread(void " ptr "[restrict ." size " * ." nmemb ],
30 .BI " size_t " size ", size_t " nmemb ,
31 .BI " FILE *restrict " stream );
32 .BI "size_t fwrite(const void " ptr "[restrict ." size " * ." nmemb ],
33 .BI " size_t " size ", size_t " nmemb ,
34 .BI " FILE *restrict " stream );
35 .fi
36 .SH DESCRIPTION
37 The function
38 .BR fread ()
39 reads
40 .I nmemb
41 items of data, each
42 .I size
43 bytes long, from the stream pointed to by
44 .IR stream ,
45 storing them at the location given by
46 .IR ptr .
47 .P
48 The function
49 .BR fwrite ()
50 writes
51 .I nmemb
52 items of data, each
53 .I size
54 bytes long, to the stream pointed to by
55 .IR stream ,
56 obtaining them from the location given by
57 .IR ptr .
58 .P
59 For nonlocking counterparts, see
60 .BR unlocked_stdio (3).
61 .SH RETURN VALUE
62 On success,
63 .BR fread ()
64 and
65 .BR fwrite ()
66 return the number of items read or written.
67 This number equals the number of bytes transferred only when
68 .I size
69 is 1.
70 If an error occurs, or the end of the file is reached,
71 the return value is a short item count (or zero).
72 .P
73 The file position indicator for the stream is advanced by the number
74 of bytes successfully read or written.
75 .P
76 .BR fread ()
77 does not distinguish between end-of-file and error, and callers must use
78 .BR feof (3)
79 and
80 .BR ferror (3)
81 to determine which occurred.
82 .SH ATTRIBUTES
83 For an explanation of the terms used in this section, see
84 .BR attributes (7).
85 .TS
86 allbox;
87 lbx lb lb
88 l l l.
89 Interface Attribute Value
90 T{
91 .na
92 .nh
93 .BR fread (),
94 .BR fwrite ()
95 T} Thread safety MT-Safe
96 .TE
97 .SH STANDARDS
98 C11, POSIX.1-2008.
99 .SH HISTORY
100 POSIX.1-2001, C89.
101 .SH EXAMPLES
102 The program below demonstrates the use of
103 .BR fread ()
104 by parsing /bin/sh ELF executable in binary mode and printing its
105 magic and class:
106 .P
107 .in +4n
108 .EX
109 $ \fB./a.out\fP
110 ELF magic: 0x7f454c46
111 Class: 0x02
112 .EE
113 .in
114 .SS Program source
115 \&
116 .\" SRC BEGIN (fread.c)
117 .EX
118 #include <stdio.h>
119 #include <stdlib.h>
120 \&
121 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
122 \&
123 int
124 main(void)
125 {
126 FILE *fp;
127 size_t ret;
128 unsigned char buffer[4];
129 \&
130 fp = fopen("/bin/sh", "rb");
131 if (!fp) {
132 perror("fopen");
133 return EXIT_FAILURE;
134 }
135 \&
136 ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fp);
137 if (ret != ARRAY_SIZE(buffer)) {
138 fprintf(stderr, "fread() failed: %zu\[rs]n", ret);
139 exit(EXIT_FAILURE);
140 }
141 \&
142 printf("ELF magic: %#04x%02x%02x%02x\[rs]n", buffer[0], buffer[1],
143 buffer[2], buffer[3]);
144 \&
145 ret = fread(buffer, 1, 1, fp);
146 if (ret != 1) {
147 fprintf(stderr, "fread() failed: %zu\[rs]n", ret);
148 exit(EXIT_FAILURE);
149 }
150 \&
151 printf("Class: %#04x\[rs]n", buffer[0]);
152 \&
153 fclose(fp);
154 \&
155 exit(EXIT_SUCCESS);
156 }
157 .EE
158 .\" SRC END
159 .SH SEE ALSO
160 .BR read (2),
161 .BR write (2),
162 .BR feof (3),
163 .BR ferror (3),
164 .BR unlocked_stdio (3)