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

NAME

6       fread - binary input
7

SYNOPSIS

9       #include <stdio.h>
10
11       size_t fread(void *restrict ptr, size_t size, size_t nitems,
12              FILE *restrict stream);
13
14

DESCRIPTION

16       The  fread() function shall read into the array pointed to by ptr up to
17       nitems elements whose size is specified by  size  in  bytes,  from  the
18       stream pointed to by stream.  For each object, size calls shall be made
19       to the fgetc() function and the results stored, in the order  read,  in
20       an array of unsigned char exactly overlaying the object. The file posi‐
21       tion indicator for the stream (if defined) shall  be  advanced  by  the
22       number  of  bytes  successfully read. If an error occurs, the resulting
23       value of the file position indicator for the stream is unspecified.  If
24       a partial element is read, its value is unspecified.
25
26       The fread() function may mark the st_atime field of the file associated
27       with stream for update. The st_atime field shall be marked  for  update
28       by  the  first  successful  execution  of  fgetc(),  fgets(), fgetwc(),
29       fgetws(), fread(), fscanf(),  getc(),  getchar(),  gets(),  or  scanf()
30       using stream that returns data not supplied by a prior call to ungetc()
31       or ungetwc().
32

RETURN VALUE

34       Upon successful completion, fread() shall return the number of elements
35       successfully  read  which  is  less than nitems only if a read error or
36       end-of-file is encountered. If size  or  nitems  is  0,  fread()  shall
37       return  0  and  the  contents  of the array and the state of the stream
38       remain unchanged. Otherwise, if a read error occurs, the error  indica‐
39       tor  for the stream shall be set,    and errno shall be set to indicate
40       the error.
41

ERRORS

43       Refer to fgetc() .
44
45       The following sections are informative.
46

EXAMPLES

48   Reading from a Stream
49       The following example reads a single element from the  fp  stream  into
50       the array pointed to by buf.
51
52
53              #include <stdio.h>
54              ...
55              size_t bytes_read;
56              char buf[100];
57              FILE *fp;
58              ...
59              bytes_read = fread(buf, sizeof(buf), 1, fp);
60              ...
61

APPLICATION USAGE

63       The ferror() or feof() functions must be used to distinguish between an
64       error condition and an end-of-file condition.
65
66       Because of possible differences in element length  and  byte  ordering,
67       files  written  using  fwrite() are application-dependent, and possibly
68       cannot be read using fread() by a different application or by the  same
69       application on a different processor.
70

RATIONALE

72       None.
73

FUTURE DIRECTIONS

75       None.
76

SEE ALSO

78       feof() , ferror() , fgetc() , fopen() , getc() , gets() , scanf() , the
79       Base Definitions volume of IEEE Std 1003.1-2001, <stdio.h>
80
82       Portions of this text are reprinted and reproduced in  electronic  form
83       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
84       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
85       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
86       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
87       event of any discrepancy between this version and the original IEEE and
88       The Open Group Standard, the original IEEE and The Open Group  Standard
89       is  the  referee document. The original Standard can be obtained online
90       at http://www.opengroup.org/unix/online.html .
91
92
93
94IEEE/The Open Group                  2003                             FREAD(P)
Impressum