22 - feof()函数

1 函数原型

feof():检查指定流stream的文件结束指示器是否被设置,函数原型如下:

int feof ( FILE * stream );

cstdio库描述如下:

1. Check end-of-file indicator
2. Checks whether the end-of-file indicator associated with stream is set, returning a value different from zero if it is.
3. This indicator is generally set by a previous operation on the stream that attempted to read at or past the end-of-file.
4. Notice that stream's internal position indicator may point to the end-of-file for the next operation, but still, the end-of-file indicator may not be set until an operation attempts to read at that point.
5. This indicator is cleared by a call to clearerr, rewind, fseek, fsetpos or freopen. Although if the position indicator is not repositioned by such a call, the next i/o operation is likely to set the indicator again.
  1. 流的文件结束指示器是在流内部维护的一个状态标志:
    (1)用于指示文件读取操作是否已经到达文件末尾,即在文件读取过程中是否尝试访问了文件末尾及其之后的位置;
    (2)文件末尾:文件的最后一个字节之后的那个虚拟位置,它并不实际存在于文件的物理存储中,而是一个逻辑上的概念;
    (3)当文件的最后一个字节被读取后,文件读写指针指向文件末尾,流的文件结束指示器并未被设置;
    (4)当尝试从文件中读取数据,且文件读写指针已指向文件末尾时,流的文件结束指示器才被设置;
    (5)通常,在读文件后,通过feof()函数来检查指定流stream的文件结束指示器是否被设置。
  2. 宏定义EOF(End of File):
    (1)EOF是cstdio库中定义的一个宏,其值通常为-1;
    (2)EOF在cstdio库中通常用作函数返回值,以指示读取操作遇到了文件末尾或读写过程遇到了错误;
    (3)如果读取操作遇到了文件末尾,会设置流的文件结束指示器(end-of-file indicator),使用feof()函数检查;
    (4)如果读写过程中遇到了错误,会设置流的错误指示器(error indicator),使用ferror()函数检查。

关于流的文件结束指示器,cstdio库描述如下:

1. Streams have certain internal indicators that specify their current state and which affect the behavior of some input and output operations performed on them.
2. Position indicator
(1) When set, indicates that the last reading or writing operation performed with the stream reached the End of File. 
(2) It can be checked with the feof function, and can be reset by calling either to clearerr or freopen or by calling to any repositioning function (rewind, fseek and fsetpos)., fseek and fsetpos.

注意 cstdio库提到 “last reading or writing operation”,流的文件结束指示器的设置主要与读文件操作相关。

2 参数

feof()函数只有一个参数stream:

  1. 参数stream是feof()函数要检查的流,类型为FILE*;stream主要是文件流,就是fopen()函数的返回值。

cstdio库描述如下:

stream
1. Pointer to a FILE object that identifies the stream.

3 返回值

feof()函数的返回值类型为int型:

  1. 如果读操作遇到文件末尾,流的文件结束指示器被设置,返回非0值;
  2. 如果读操作未到文件末尾,流的文件结束指示器未设置,返回0值。

cstdio库描述如下:

1. A non-zero value is returned in the case that the end-of-file indicator associated with the stream is set.
2. Otherwise, zero is returned.

4 示例

示例代码如下所示:

int main()
{
   //
   FILE* fpr1 = NULL;
   FILE* fpw2 = NULL;
   FILE* fpw3 = NULL;
   char ch = 0;
   int n = 0;
   //
   fpr1 = fopen("1.txt", "r");
   fpw2 = fopen("2.txt", "w");
   fpw3 = fopen("3.txt", "w");

   if (fpr1 == NULL || fpw2 == NULL || fpw3 == NULL)
   {
      perror("Failed to open file ");
      exit(1);
   }
   //
   while (feof(fpr1) == 0)
   {
      fputc(fgetc(fpr1), fpw2);
   }
   //
   rewind(fpr1);
   //
   while (1)
   {
      //
      ch = fgetc(fpr1);
      //
      if (feof(fpr1) == 0)
      {
         fputc(ch, fpw3);
      }
      else
      {
         break;
      }
   }
   //
   fclose(fpr1);
   fclose(fpw2);
   fclose(fpw3);
   //
   return 0;
}

文件内容如下图所示:

在这里插入图片描述

代码运行结果如下图所示:

在这里插入图片描述

在这里插入图片描述

代码及运行结果分析如下:

  1. 先判断feof(),再读文件1.txt,后写文件2.txt,文件2.txt比文件1.txt多一个字节,值为0xFF;
  2. 先读文件1.txt,再判断feof(),后写文件3.txt,文件3.txt与文件1.txt完全一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值