C 库函数 - feof()

C 语言教程 · 2019-02-23 13:57:10

描述

C 库函数 int feof(FILE *stream) 测试给定流 stream 的文件结束标识符。

声明

下面是 feof() 函数的声明。

int feof(FILE *stream)

参数

  • stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。

返回值

当设置了与流关联的文件结束标识符时,该函数返回一个非零值,否则返回零。

实例

下面的实例演示了 feof() 函数的用法。

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;
  
   fp = fopen("file.txt","r");
   if(fp == NULL) 
   {
      perror("打开文件时发生错误");
      return(-1);
   }
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      { 
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

假设我们有一个文本文件 file.txt,它的内容如下所示。该文件将作为我们实例程序中的一个输入使用:

这里是 codercto.com

让我们编译并运行上面的程序,这将产生以下结果:

这里是 codercto.com

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Graph Algorithms

Graph Algorithms

Shimon Even / Cambridge University Press / 2011-9-19 / USD 32.99

Shimon Even's Graph Algorithms, published in 1979, was a seminal introductory book on algorithms read by everyone engaged in the field. This thoroughly revised second edition, with a foreword by Richa......一起来看看 《Graph Algorithms》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试