Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 1

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 1

Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 8

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 8

fread()

Read elements of a given size from a stream

Synopsis:

#include <stdio.h>

size_t fread( void* buf,
              size_t size,
              size_t num,
              FILE* fp );

Arguments:

buf
A pointer to a buffer where the function can store the elements that it reads.
size
The size of each element to read.
num
The number of elements to read.
fp
The stream from which to read the elements.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The fread() function reads num elements of size bytes each from the stream specified by fp into the buffer specified by buf.

Returns:

The number of complete elements successfully read; this value may be less than the requested number of elements.

Use the feof() and ferror() functions to determine whether the end of the file was encountered or if an input/output error has occurred.

Errors:

If an error occurs, errno is set to indicate the type of error.

Examples:

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>
#include <stdlib.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( struct student_data ), 1, fp ) );
}

int main( void )
{
    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 );
        
        return EXIT_SUCCESS;
    }
    
    return EXIT_FAILURE;
}

Classification:

ANSI, POSIX 1003.1

Safety:
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes

See also:

errno, fopen(), feof(), ferror()


Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 188

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/f/fread.html on line 188