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.Dd April 2, 2022 33.Dt FREAD 3 34.Os 35.Sh NAME 36.Nm fread , 37.Nm fread_unlocked , 38.Nm fwrite , 39.Nm fwrite_unlocked 40.Nd binary stream input/output 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In stdio.h 45.Ft size_t 46.Fn fread "void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream" 47.Ft size_t 48.Fn fread_unlocked "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.Ft size_t 52.Fn fwrite_unlocked "const void * restrict ptr" "size_t size" "size_t nmemb" "FILE * restrict stream" 53.Sh DESCRIPTION 54The function 55.Fn fread 56reads 57.Fa nmemb 58objects, each 59.Fa size 60bytes long, from the stream pointed to by 61.Fa stream , 62storing them at the location given by 63.Fa ptr . 64.Pp 65The function 66.Fn fwrite 67writes 68.Fa nmemb 69objects, each 70.Fa size 71bytes long, to the stream pointed to by 72.Fa stream , 73obtaining them from the location given by 74.Fa ptr . 75.Pp 76The 77.Fn fread_unlocked 78and 79.Fn fwrite_unlocked 80functions are equivalent to 81.Fn fread 82and 83.Fn fwrite 84respectively, except that the caller is responsible for locking the stream 85with 86.Xr flockfile 3 87before calling them. 88These functions may be used to avoid the overhead of locking the stream 89and to prevent races when multiple threads are operating on the same stream. 90.Sh RETURN VALUES 91The functions 92.Fn fread 93and 94.Fn fwrite 95advance the file position indicator for the stream 96by the number of bytes read or written. 97They return the number of objects read or written. 98If an error occurs, or the end-of-file is reached, 99the return value is a short object count (or zero). 100.Pp 101The function 102.Fn fread 103does not distinguish between end-of-file and error, and callers 104must use 105.Xr feof 3 106and 107.Xr ferror 3 108to determine which occurred. 109The function 110.Fn fwrite 111returns a value less than 112.Fa nmemb 113only if a write error has occurred. 114.Sh SEE ALSO 115.Xr read 2 , 116.Xr write 2 117.Sh STANDARDS 118The functions 119.Fn fread 120and 121.Fn fwrite 122conform to 123.St -isoC . 124.Sh HISTORY 125The functions 126.Fn fread 127and 128.Fn fwrite 129first appeared in 130.At v7 . 131