The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning nonzero if it is set. The end-of-file indicator can be cleared only by the function clearerr(). In below program, we read the file myfile.txt till the end of file has been detected and count the number of bytes.

Command line execution

Terminalbash
gcc -o feof feof.c Make sure myfile.txt file is present of same directory as executable and Run the executable as, $ ./feof  Number of bytes read = 581

Implementation details

This can help us to measure the file length programatically. #include <stdio.h> #include <stdlib.h> void main (int argc, char **argv) { int count, total = 0; char buffer[100]; FILE *stream; if( (stream = fopen( "myfile.txt", "r" )) == NULL ) { printf("file not found\n"); exit( 1 ); } /* 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 ) ) { perror( "Read error" ); break; } /* Total up actual bytes read */ total += count; } printf( "Number of bytes read = %d\n", total ); fclose( stream ); } compile this code as, $ gcc -o feof feof.c Make sure myfile.txt file is present of same directory as executable and Run the executable as, $ ./feof Number of bytes read = 581

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for feof() end-of-file indicator function in c – example.