xref: /openbsd-src/lib/libc/stdio/fread.3 (revision 667c0cc9cc94a1e8c0fb440a88761b2d04a9f410)
1.\"	$OpenBSD: fread.3,v 1.11 2015/03/12 02:16:29 lteo Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  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.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
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. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd $Mdocdate: March 12 2015 $
35.Dt FREAD 3
36.Os
37.Sh NAME
38.Nm fread ,
39.Nm fwrite
40.Nd binary stream input/output
41.Sh SYNOPSIS
42.In stdio.h
43.Ft size_t
44.Fn fread "void *ptr" "size_t size" "size_t nmemb" "FILE *stream"
45.Ft size_t
46.Fn fwrite "const void *ptr" "size_t size" "size_t nmemb" "FILE *stream"
47.Sh DESCRIPTION
48The function
49.Fn fread
50reads
51.Fa nmemb
52objects, each
53.Fa size
54bytes long, from the stream pointed to by
55.Fa stream ,
56storing them at the location given by
57.Fa ptr .
58.Pp
59The function
60.Fn fwrite
61writes
62.Fa nmemb
63objects, each
64.Fa size
65bytes long, to the stream pointed to by
66.Fa stream ,
67obtaining them from the location given by
68.Fa ptr .
69.Sh RETURN VALUES
70The functions
71.Fn fread
72and
73.Fn fwrite
74advance the file position indicator for the stream
75by the number of bytes read or written.
76They return the number of objects read or written.
77If
78.Fa size
79or
80.Fa nmemb
81is 0,
82.Fn fread
83and
84.Fn fwrite
85return 0 with no change made to the
86.Fa stream .
87If the product of
88.Fa size
89and
90.Fa nmemb
91results in integer overflow, 0 is returned and errno
92is set to
93.Er EOVERFLOW .
94If an error occurs, or the end-of-file is reached,
95the return value is a short object count (or zero).
96.Pp
97The function
98.Fn fread
99does not distinguish between end-of-file and error, and callers
100must use
101.Xr feof 3
102and
103.Xr ferror 3
104to determine which occurred.
105The function
106.Fn fwrite
107returns a value less than
108.Fa nmemb
109only if a write error has occurred.
110.Sh SEE ALSO
111.Xr read 2 ,
112.Xr write 2
113.Sh STANDARDS
114The functions
115.Fn fread
116and
117.Fn fwrite
118conform to
119.St -ansiC .
120.Sh HISTORY
121The functions
122.Fn fread
123and
124.Fn fwrite
125first appeared in
126.At v7 .
127