Test if the end-of-file has been reached
#include <unistd.h> int eof( int filedes );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The eof() function is a low-level function that determines if the end of the file specified by filedes has been reached.
Input operations set the current file position; you can call the eof() function to detect the end of the file before more input operations to prevent attempts at reading beyond the end of the file.
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> int main( void ) { int filedes , len; char buffer[100]; filedes = open( "file", O_RDONLY ); if( filedes != -1 ) { while( ! eof( filedes ) ) { len = read( filedes , buffer, sizeof(buffer) - 1 ); buffer[ len ] = '\0'; printf( "%s", buffer ); } close( filedes ); return EXIT_SUCCESS; } return EXIT_FAILURE; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |