1.\" Copyright (c) 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" Chris Torek and the American National Standards Committee X3, 6.\" on Information Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)fread.3 8.2 (Berkeley) 3/8/94 33.\" 34.Dd April 2, 2022 35.Dt FREAD 3 36.Os 37.Sh NAME 38.Nm fread , 39.Nm fread_unlocked , 40.Nm fwrite , 41.Nm fwrite_unlocked 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 fread_unlocked "void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream" 51.Ft size_t 52.Fn fwrite "const void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream" 53.Ft size_t 54.Fn fwrite_unlocked "const void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream" 55.Sh DESCRIPTION 56The function 57.Fn fread 58reads 59.Fa nmemb 60objects, each 61.Fa size 62bytes long, from the stream pointed to by 63.Fa stream , 64storing them at the location given by 65.Fa ptr . 66.Pp 67The function 68.Fn fwrite 69writes 70.Fa nmemb 71objects, each 72.Fa size 73bytes long, to the stream pointed to by 74.Fa stream , 75obtaining them from the location given by 76.Fa ptr . 77.Pp 78The 79.Fn fread_unlocked 80and 81.Fn fwrite_unlocked 82functions are equivalent to 83.Fn fread 84and 85.Fn fwrite 86respectively, except that the caller is responsible for locking the stream 87with 88.Xr flockfile 3 89before calling them. 90These functions may be used to avoid the overhead of locking the stream 91and to prevent races when multiple threads are operating on the same stream. 92.Sh RETURN VALUES 93The functions 94.Fn fread 95and 96.Fn fwrite 97advance the file position indicator for the stream 98by the number of bytes read or written. 99They return the number of objects read or written. 100If an error occurs, or the end-of-file is reached, 101the return value is a short object count (or zero). 102.Pp 103The function 104.Fn fread 105does not distinguish between end-of-file and error, and callers 106must use 107.Xr feof 3 108and 109.Xr ferror 3 110to determine which occurred. 111The function 112.Fn fwrite 113returns a value less than 114.Fa nmemb 115only if a write error has occurred. 116.Sh SEE ALSO 117.Xr read 2 , 118.Xr write 2 119.Sh STANDARDS 120The functions 121.Fn fread 122and 123.Fn fwrite 124conform to 125.St -isoC . 126.Sh HISTORY 127The functions 128.Fn fread 129and 130.Fn fwrite 131first appeared in 132.At v7 . 133