1FREAD(3P)                  POSIX Programmer's Manual                 FREAD(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       fread — binary input
14

SYNOPSIS

16       #include <stdio.h>
17
18       size_t fread(void *restrict ptr, size_t size, size_t nitems,
19           FILE *restrict stream);
20

DESCRIPTION

22       The functionality described on this reference page is aligned with  the
23       ISO C  standard.  Any  conflict between the requirements described here
24       and the ISO C standard is unintentional. This  volume  of  POSIX.1‐2008
25       defers to the ISO C standard.
26
27       The  fread() function shall read into the array pointed to by ptr up to
28       nitems elements whose size is specified by  size  in  bytes,  from  the
29       stream pointed to by stream.  For each object, size calls shall be made
30       to the fgetc() function and the results stored, in the order  read,  in
31       an array of unsigned char exactly overlaying the object. The file posi‐
32       tion indicator for the stream (if defined) shall  be  advanced  by  the
33       number  of  bytes  successfully read. If an error occurs, the resulting
34       value of the file position indicator for the stream is unspecified.  If
35       a partial element is read, its value is unspecified.
36
37       The  fread()  function  may  mark the last data access timestamp of the
38       file associated with stream for update. The last data access  timestamp
39       shall  be  marked  for  update  by  the  first  successful execution of
40       fgetc(), fgets(), fread(),  fscanf(),  getc(),  getchar(),  getdelim(),
41       getline(),  gets(),  or scanf() using stream that returns data not sup‐
42       plied by a prior call to ungetc().
43

RETURN VALUE

45       Upon successful completion, fread() shall return the number of elements
46       successfully  read  which  is  less than nitems only if a read error or
47       end-of-file is encountered. If size  or  nitems  is  0,  fread()  shall
48       return  0  and  the  contents  of the array and the state of the stream
49       remain unchanged. Otherwise, if a read error occurs, the error  indica‐
50       tor for the stream shall be set, and errno shall be set to indicate the
51       error.
52

ERRORS

54       Refer to fgetc().
55
56       The following sections are informative.
57

EXAMPLES

59   Reading from a Stream
60       The following example reads a single element from the  fp  stream  into
61       the array pointed to by buf.
62
63           #include <stdio.h>
64           ...
65           size_t elements_read;
66           char buf[100];
67           FILE *fp;
68           ...
69           elements_read = fread(buf, sizeof(buf), 1, fp);
70           ...
71
72       If  a  read  error occurs, elements_read will be zero but the number of
73       bytes  read  from  the  stream  could  be   anything   from   zero   to
74       sizeof(buf)−1.
75
76       The  following  example reads multiple single-byte elements from the fp
77       stream into the array pointed to by buf.
78
79           #include <stdio.h>
80           ...
81           size_t bytes_read;
82           char buf[100];
83           FILE *fp;
84           ...
85           bytes_read = fread(buf, 1, sizeof(buf), fp);
86           ...
87
88       If a read error occurs, bytes_read will contain  the  number  of  bytes
89       read from the stream.
90

APPLICATION USAGE

92       The ferror() or feof() functions must be used to distinguish between an
93       error condition and an end-of-file condition.
94
95       Because of possible differences in element length  and  byte  ordering,
96       files  written  using  fwrite() are application-dependent, and possibly
97       cannot be read using fread() by a different application or by the  same
98       application on a different processor.
99

RATIONALE

101       None.
102

FUTURE DIRECTIONS

104       None.
105

SEE ALSO

107       Section  2.5, Standard I/O Streams, feof(), ferror(), fgetc(), fopen(),
108       fscanf(), getc(), gets()
109
110       The Base Definitions volume of POSIX.1‐2008, <stdio.h>
111
113       Portions of this text are reprinted and reproduced in  electronic  form
114       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
115       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
116       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
117       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
118       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
119       event of any discrepancy between this version and the original IEEE and
120       The  Open Group Standard, the original IEEE and The Open Group Standard
121       is the referee document. The original Standard can be  obtained  online
122       at http://www.unix.org/online.html .
123
124       Any  typographical  or  formatting  errors that appear in this page are
125       most likely to have been introduced during the conversion of the source
126       files  to  man page format. To report such errors, see https://www.ker
127       nel.org/doc/man-pages/reporting_bugs.html .
128
129
130
131IEEE/The Open Group                  2013                            FREAD(3P)
Impressum