Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C Library Reference - <u>synopsis:</u> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Synopsis:
    #include <stdio.h>
    size_t fread( void *buf,
                  size_t elsize,
                  size_t nelem,
                  FILE *fp );

Description:
    The fread function reads nelem elements of elsize bytes each from the
    file specified by fp into the buffer specified by buf.

Returns:
    The fread function returns the number of complete elements successfully
    read.  This value may be less than the requested number of elements.

    The  feof and  ferror functions can be used to determine whether the end
    of the file was encountered or if an input/output error has occurred.
     When an error has occurred,  errno contains a value indicating the type
    of error that has been detected.

Example:
    The following example reads a simple student record containing binary
    data.  The student record is described by the struct student_data
    declaration.

    #include <stdio.h>

    struct student_data {
        int  student_id;
        unsigned char marks[10];
    };

    size_t read_data( FILE *fp, struct student_data *p )
      {
        return( fread( p, sizeof(*p), 1, fp ) );
      }

    void main()
      {
        FILE *fp;
        struct student_data std;
        int i;

        fp = fopen( "file", "r" );
        if( fp != NULL ) {
          while( read_data( fp, &std ) != 0 ) {
            printf( "id=%d ", std.student_id );
            for( i = 0; i < 10; i++ )
              printf( "%3d ", std.marks[ i ] );
            printf( "\n" );
          }
          fclose( fp );
        }
      }

Classification:
    ANSI

Systems:
    All, Netware

See Also:
    fopen, feof, ferror

See Also: feof ferror

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson