Function name : eof
Header file : <io.h>
Function prototype : int eof(int handle);
Function : Check if the file is ended
Parameters : int handle is the file handle to be detected
Return value : Returns 1 when the end of the file is reached, 0 when the end of the file is not reached, and -1 when an error occurs.
Program example : Use the read function to read data from the file, and use the eof function to detect the end of the file.
#include<stdio.h>#include<io.h>#include<fcntl.h>intmain(void){intfd=open(D:\a.txt,_O_RDWR+O_CREAT);if(fd==-1 ){printf(cannotcreatethefil en);return1;}charbuf[81];intn;while(!eof(fd)){//Check whether the end of the file is read n=read(fd,buf,80);//Read from the file Take 80 characters buf[n]=0;printf(%sn,buf);}close(fd);return0;}
Running results
www.dotcpp.comwww.dotcpp.comwww.dotcpp.com