函数名:eof
头文件:<io.h>
函数原型: int eof(int handle);
功能: 检查文件是否结束
参数: int handle 为要检测的文件句柄
返回值: 到文件末尾返回1,没到文件末尾返回0,出错返回-1
程序例: 使用read函数从文件读数据,用eof函数检测文件尾
#include<stdio.h> #include<io.h> #include<fcntl.h> int main(void){ int fd=open("D:\\a.txt",_O_RDWR+O_CREAT); if(fd==-1){ printf("can not create the file\n"); return 1; } char buf[81]; int n; while(!eof(fd)){ //检测是否读到文件的末尾 n=read(fd,buf,80); //从文件中读取80个字符 buf[n]=0; printf("%s\n",buf); } close(fd); return 0; }
运行结果
www.dotcpp.com www.dotcpp.com www.dotcpp.com
本文固定URL:https://www.dotcpp.com/course/456