fread


Standard I/O Functions                                  fread(3S)



NAME
     fread, fwrite - buffered binary input/output

SYNOPSIS
     #include 

     size_t fread(void *ptr, size_t  size,  size_t  nitems,  FILE
     *stream);

     size_t fwrite(const void *ptr, size_t size,  size_t  nitems,
     FILE *stream);

DESCRIPTION
     The fread() function reads into an array pointed to  by  _p_t_r
     up  to  _n_i_t_e_m_s  items  of data from _s_t_r_e_a_m, where an item of
     data is a sequence of bytes (not necessarily terminated by a
     null  byte)  of  length  _s_i_z_e.  It stops reading bytes if an
     end-of-file or error condition is encountered while  reading
     _s_t_r_e_a_m, or if _n_i_t_e_m_s items have been read. It increments the
     data pointer in _s_t_r_e_a_m to point to the  byte  following  the
     last  byte read if there is one. It does not change the con-
     tents of _s_t_r_e_a_m. It returns the number of items read.

     The fwrite() function writes to the named output  _s_t_r_e_a_m  at
     most  _n_i_t_e_m_s items of data from the array pointed to by _p_t_r,
     where an item of data is a sequence  of  bytes  (not  neces-
     sarily  terminated  by a null byte) of length _s_i_z_e. It stops
     writing when it has written _n_i_t_e_m_s items of data  or  if  an
     error condition is encountered on _s_t_r_e_a_m. It does not change
     the contents of the array pointed to by _p_t_r.  It  increments
     the  data  pointer  in _s_t_r_e_a_m by the number of bytes written
     and returns the number of items written.

     A call to fwrite() in buffered mode may return  sucess  even
     though  the  underlying  call  to  write(2) fails.  This can
     cause unpredicable results. Use either the write()  function
     or   the   fwrite()   function   in  unbuffered  mode.   See
     setvbuf(3S).

     The ferror() or feof() routines must be used to  distinguish
     between  an  error condition and end-of-file condition.  See
     ferror(3S).

RETURN VALUES
     The fread() function returns the number of items  read.  The
     fwrite() function returns the number of items written.

     If _s_i_z_e or _n_i_t_e_m_s is 0, then fread() and fwrite()  return  0
     and do not effect the state of _s_t_r_e_a_m.

     If an error occurs,  fread() and  fwrite() return 0 and  set
     the error indicator for _s_t_r_e_a_m.



SunOS 5.7           Last change: 11 Apr 1997                    1






Standard I/O Functions                                  fread(3S)



ERRORS
     The fread() function will fail if data needs to be read and:

     EOVERFLOW The file is a regular file, _s_i_z_e is  greater  than
               0,  the  starting  position  is before the end-of-
               file, and an attempt was made to read at or beyond
               the offset maximum associated with the correspond-
               ing _s_t_r_e_a_m.

     The fwrite() function will fail  if  either  the  _s_t_r_e_a_m  is
     unbuffered or the _s_t_r_e_a_m's buffer needed to be flushed and:

     EFBIG     The file is a regular file and an attempt was made
               to write at or beyond the offset maximum.

ATTRIBUTES
     See attributes(5) for descriptions of the  following  attri-
     butes:
     ____________________________________________________________
    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    |______________________________|______________________________|
    | MT-Level                    | MT-Safe                     |
    |______________________________|______________________________|

SEE ALSO
     read(2),  write(2),   fclose(3S),   ferror(3S),   fopen(3S),
     getc(3S),    gets(3S),   printf(3S),   putc(3S),   puts(3S),
     scanf(3S), setvbuf(3S), stdio(3S), attributes(5)



SunOS 5.7           Last change: 11 Apr 1997                    2



Last modified: Fri Aug 20 09:06:00 BRT 2004