xref: /openbsd-src/lib/libc/stdio/fread.3 (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1.\"	$OpenBSD: fread.3,v 1.5 2009/07/12 18:45:21 millert 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: July 12 2009 $
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.Fd #include <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 an error occurs, or the end-of-file is reached,
88the return value is a short object count (or zero).
89.Pp
90The function
91.Fn fread
92does not distinguish between end-of-file and error, and callers
93must use
94.Xr feof 3
95and
96.Xr ferror 3
97to determine which occurred.
98The function
99.Fn fwrite
100returns a value less than
101.Fa nmemb
102only if a write error has occurred.
103.Sh SEE ALSO
104.Xr read 2 ,
105.Xr write 2
106.Sh STANDARDS
107The functions
108.Fn fread
109and
110.Fn fwrite
111conform to
112.St -ansiC .
113