文件结束符

#include <stdio.h>
int main(int argc, char* argv[])
{
        if(argc<2||argc >3)
                printf("usage:%s filename/n",argv[0]);

        FILE * fp = NULL;
        fp = fopen(argv[1],"r");
        if (fp==NULL)
                printf("open file error/n");

        printf("EOF=%x/n",EOF);
        char c;
#if 0

       //正确方法
        c=fgetc(fp);
        while(!feof(fp))
        {
               
                printf("[%x]/n",c);
                c=fgetc(fp);
        }
#endif
#if 0

       //比原文件多一个-1;

       //例如末尾为0x33,当读到0x33后,打印出来进入下一个循环,feof此时fp->flag还没有被设置_IOEOF,仍会调用fgetc,在这里fp->flag被设置_IOEOF标志,此时fgetc返回的是-1,打印出c后再调用feof,此时才判断到了文件末尾,退出循环。

        while(!feof(fp))
        {
                c=fgetc(fp);
                printf("[%x]/n",c);
        }

#endif
#if 0
        //EOF紧用于文本文件。

        c=fgetc(fp);
        while(c!=EOF)
        {
               
                printf("[%x]/n",c);
                c=fgetc(fp);
        }
#endif
        return 0;
}

查看 stdio.h 可以看到如下定义:

#define  EOF  (-1)

#define  _IOEOF  0x0010
#define  feof(_stream)  ((_stream)->_flag & _IOEOF)

由此可以看出,这两种方式的原理是不同的。

在这里先说下EOF和feof()这个两个宏定义,EOF是不可输出字符,因此不能在屏幕上显示。由于字符的ASCII码不可能出现-1,因此EOF定义为-1是合适的。当读入的字符值等于EOF时,表示读入的已不是正常的字符而是文件结束符,但这适用对文本文件的读写。在二进制文件中,信息都是以数值方式存在的。EOF的值可能就是所要处理的二进制文件中的信息。这就出现了需要读入有用数据却被处理为“文件结束“的情况。为了解决这个问题,C提供了一个feof()函数,可以用它来判断文件是否结束。feof(fp)用于测试fp所指向的文件的当前状态是否为“文件结束”。如果是,函数则返回的值是1(真),否则为0(假)。

只有当文件位置指针(fp->_ptr)到了文件末尾,然后再发生读/写操作时,标志位(fp->_flag)才会被置为含有_IOEOF。然后再调用feof(),才会得到文件结束的信息。

 

使用feof需要注意的是fseek会清除_IOEOF标志:

int fseek ( FILE * stream, long int offset, int origin ); <cstdio>

Reposition stream position indicator

Sets the position indicator associated with the stream to a new position defined by adding offset to a reference position specified by origin.The End-of-File internal indicator of the stream is cleared after a call to this function, and all effects from previous calls to ungetc are dropped.When using fseek on text files with offset values other than zero or values retrieved with ftell, bear in mind that on some platforms some format transformations occur with text files which can lead to unexpected repositioning.On streams open for update (read+write), a call to fseek allows to switch between reading and writing.

 

feof

Tests for end-of-file on a stream.

int feof( FILE *stream );

Return Value

The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.

Parameter

stream -> Pointer to FILE structure

Remarks

The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
遇到文件结束符停止线程是一个配置选项,它决定了当CSV数据文件读取完毕后,是否停止当前线程。如果设置为true,那么线程会在读取完数据后停止。如果设置为false,线程会继续执行其他操作。这个选项可以根据具体需求来设置,如果希望线程在读取完数据后停止,可以将其设置为true。\[1\] #### 引用[.reference_title] - *1* [jmeter记录二](https://blog.csdn.net/m0_57133702/article/details/120583727)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Jmeter之参数传递的三种方式](https://blog.csdn.net/weixin_43641920/article/details/126312162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [高级性能测试系列《18.csv数据文件设置:遇到文件结束符再次...遇到文件结束符停止线程?线程共享模式。》...](https://blog.csdn.net/weixin_41033105/article/details/125383615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值