Dotcpp  >  编程教程  >  io.h头文件  >  C语言eof()函数:检查文件是否结束

C语言eof()函数:检查文件是否结束

点击打开在线编译器,边学边练

函数名: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

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)