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

NAME

12       fread - binary input
13

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

49       Refer to fgetc().
50
51       The following sections are informative.
52

EXAMPLES

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

APPLICATION USAGE

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

RATIONALE

78       None.
79

FUTURE DIRECTIONS

81       None.
82

SEE ALSO

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