1 .\" Copyright (c) 1990, 1991 The Regents of the University of California.
2 .\" and Copyright (c) 2020 Arkadiusz Drabczyk <arkadiusz@drabczyk.org>
3 .\" All rights reserved.
5 .\" This code is derived from software contributed to Berkeley by
6 .\" Chris Torek and the American National Standards Committee X3,
7 .\" on Information Processing Systems.
9 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. All advertising materials mentioning features or use of this software
19 .\" must display the following acknowledgement:
20 .\" This product includes software developed by the University of
21 .\" California, Berkeley and its contributors.
22 .\" 4. Neither the name of the University nor the names of its contributors
23 .\" may be used to endorse or promote products derived from this software
24 .\" without specific prior written permission.
26 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 .\" @(#)fread.3 6.6 (Berkeley) 6/29/91
41 .\" Converted for Linux, Mon Nov 29 15:37:33 1993, faith@cs.unc.edu
42 .\" Sun Feb 19 21:26:54 1995 by faith, return values
43 .\" Modified Thu Apr 20 20:43:53 1995 by Jim Van Zandt <jrv@vanzandt.mv.com>
44 .\" Modified Fri May 17 10:21:51 1996 by Martin Schulze <joey@infodrom.north.de>
46 .TH FREAD 3 2021-03-22 "GNU" "Linux Programmer's Manual"
48 fread, fwrite \- binary stream input/output
53 .BI "size_t fread(void *restrict " ptr ", size_t " size ", size_t " nmemb ,
54 .BI " FILE *restrict " stream );
55 .BI "size_t fwrite(const void *restrict " ptr ", size_t " size \
57 .BI " FILE *restrict " stream );
66 bytes long, from the stream pointed to by
68 storing them at the location given by
77 bytes long, to the stream pointed to by
79 obtaining them from the location given by
82 For nonlocking counterparts, see
83 .BR unlocked_stdio (3).
89 return the number of items read or written.
90 This number equals the number of bytes transferred only when
93 If an error occurs, or the end of the file is reached,
94 the return value is a short item count (or zero).
96 The file position indicator for the stream is advanced by the number
97 of bytes successfully read or written.
100 does not distinguish between end-of-file and error, and callers must use
104 to determine which occurred.
106 For an explanation of the terms used in this section, see
114 Interface Attribute Value
118 T} Thread safety MT-Safe
124 POSIX.1-2001, POSIX.1-2008, C89.
126 The program below demonstrates the use of
128 by parsing /bin/sh ELF executable in binary mode and printing its
134 ELF magic: 0x7f454c46
144 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
149 FILE *fp = fopen("/bin/sh", "rb");
155 unsigned char buffer[4];
157 size_t ret = fread(buffer, sizeof(*buffer), ARRAY_SIZE(buffer), fp);
158 if (ret != ARRAY_SIZE(buffer)) {
159 fprintf(stderr, "fread() failed: %zu\en", ret);
163 printf("ELF magic: %#04x%02x%02x%02x\en", buffer[0], buffer[1],
164 buffer[2], buffer[3]);
166 ret = fread(buffer, 1, 1, fp);
168 fprintf(stderr, "fread() failed: %zu\en", ret);
172 printf("Class: %#04x\en", buffer[0]);
184 .BR unlocked_stdio (3)