2.1.9.3 ferror


Description

The ferror routine tests for a reading or writing error on the file associated with stream. If an error has occurred, the error indicator for the stream remains set until the stream is closed or rewound, or until clearerr is called against it.

Syntax

int ferror( FILE * stream )

Parameters

stream
Pointer to FILE

Return

If no error has occurred on stream, ferror returns 0. Otherwise, it returns a nonzero value.

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_ferror()
{
	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

feof, fopen

Header to Include

origin.h

Reference