xref: /minix/lib/libc/stdio/fread.3 (revision ebfedea0)
1.\"	$NetBSD: fread.3,v 1.15 2011/09/11 04:55:48 jruoho 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.\"     @(#)fread.3	8.2 (Berkeley) 3/8/94
35.\"
36.Dd September 11, 2011
37.Dt FREAD 3
38.Os
39.Sh NAME
40.Nm fread ,
41.Nm fwrite
42.Nd binary stream input/output
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft size_t
48.Fn fread "void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream"
49.Ft size_t
50.Fn fwrite "const void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream"
51.Sh DESCRIPTION
52The function
53.Fn fread
54reads
55.Fa nmemb
56objects, each
57.Fa size
58bytes long, from the stream pointed to by
59.Fa stream ,
60storing them at the location given by
61.Fa ptr .
62.Pp
63The function
64.Fn fwrite
65writes
66.Fa nmemb
67objects, each
68.Fa size
69bytes long, to the stream pointed to by
70.Fa stream ,
71obtaining them from the location given by
72.Fa ptr .
73.Pp
74Mixing
75.Fn fread
76and
77.Fn fwrite
78calls without setting the file position explicitly using
79.Xr fsetpos 3
80between read and write or write and read operations will lead to unexpected
81results because of buffering the file pointer not being set to the
82expected position after each operation completes.
83This behavior is allowed by ANSI C for efficiency and it will not be changed.
84.Sh RETURN VALUES
85The functions
86.Fn fread
87and
88.Fn fwrite
89advance the file position indicator for the stream
90by the number of bytes read or written.
91They return the number of objects read or written.
92If
93.Fa size
94or
95.Fa nmemb
96is 0, the functions return 0 and the state of
97.Fa stream
98remains unchanged.
99If an error occurs, or the end-of-file is reached,
100the return value is a short object count (or zero).
101.Pp
102The function
103.Fn fread
104does not distinguish between end-of-file and error, and callers
105must use
106.Xr feof 3
107and
108.Xr ferror 3
109to determine which occurred.
110The function
111.Fn fwrite
112returns a value less than
113.Fa nmemb
114only if a write error has occurred.
115.Sh SEE ALSO
116.Xr read 2 ,
117.Xr write 2
118.Sh STANDARDS
119The functions
120.Fn fread
121and
122.Fn fwrite
123conform to
124.St -ansiC .
125