feof

 

Description

The feof routine determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed.

Syntax

int feof( FILE * stream )

Parameters

stream
Pointer to FILE

Return

Returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.

Examples

EX1

//The following example uses feof to indicate when it reaches the end of your current C file. It also checks for errors with ferror.

void test_feof()
{
        int  count, total = 0;
        char buffer[100];
        FILE *stream;
        
        // open your current using C file for read
        stream = fopen( __FILE__, "r" );
        if(stream == NULL )
                printf( "The current C file was not opened\n" );
        
        // Cycle until end of file reached: 
        while( !feof( stream ) )
        {
                // Attempt to read in 10 bytes: 
                count = fread( buffer, sizeof( char ), 100, stream );
                if( ferror( stream ) )
                    printf("Read Error");
                
                // Total up actual bytes read 
                total += count;
        }
        
        printf( "Number of bytes read = %d\n", total );
        fclose( stream );
}

Remark

See Also

ferror

header to Include

origin.h

Reference