从文件中读取和使用的feof退出循环() [英] Reading from file and exiting loop using feof()

查看:174
本文介绍了从文件中读取和使用的feof退出循环()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此链接讲述为什么的feof()是作为一个循环退出的指标,使用一件坏事。

不安全==>有一个的feof()检查的同时,和与fgets()里面的而。

安全==具有与fgets()>!= NULL 检查的同时,本身。

我应该看到不安全code做一个额外的,而循环迭代,但都做同样的(正确)的数量的循环。有人可以帮助我了解这里发生了什么?

编辑:链接居然说为什么发生这种情况,但它采取了下面,我明白正是我一直在阅读正确的答案。我的文件不具有的'\\ n'在最后一行,所以得到了相同的结果。

这是该文件的内容:

  ABCD
EFGH
IJKL

和这里的code:

 无效testUnsafe(无效){
    FILE * F;
    焦炭BUF [20];
    F =的fopen(fil.txt,R);
    而(!的feof(F)){
        与fgets(BUF,20,F);
        如果(BUF [strlen的(BUF) - 1] =='\\ n')//清洁剂
            BUF [strlen的(BUF) - 1] ='\\ 0';
        的printf(%S,%d个\\ N,BUF,strlen的(BUF));
    }
    FCLOSE(F);
}无效TESTSAFE(无效){
    FILE * F;
    焦炭BUF [20];
    F =的fopen(fil.txt,R);
    而(与fgets(BUF,20,F)!= NULL){
        如果(BUF [strlen的(BUF) - 1] =='\\ n')//清洁剂
            BUF [strlen的(BUF) - 1] ='\\ 0';
        的printf(%S,%d个\\ N,BUF,strlen的(BUF));
    }
    FCLOSE(F);
}

输出是:

  ******不安全测试********
ABCD,4
EFGH,4
IJKL,4
********安全测试********
ABCD,4
EFGH,4
IJKL,4


解决方案

如果您的文本文件,结束的没有的文本的最后一行后换行,那么 testUnsafe()函数将达到文件结束,当它读取的最后一行,并产生输出的三条线,你表现出来了。

如果您的文本文件中的确实的有文字的最后一行后换行,该函数将读取的最后一行,包括换行符,的没有的达到最终OF-文件。当再次进入而()循环,它读取零字符,设置结束文件的标志,并输出最后一行这仍然是在缓冲从最后一轮。

而(!的feof(F))建设本身不是不安全的 的。它忽略了检查与fgets的返回值()这是不安全的。

This link tells about why feof() is a bad thing to use as an exit indicator for a loop.

Unsafe ==> having an feof() check in the while, and an fgets() inside the while.

Safe ==> having the fgets()!=NULL check in the while itself.

I'm supposed to see the unsafe code doing an extra while loop iteration, but both do the same(and correct) number of loops. Can someone help me understand what's happening here ?

EDIT : The link actually did say why this is happening, but it took for the correct answer below for me to understand exactly what i was reading. My file did not have a '\n' at the last line, so got same results.

This is the file contents :

abcd
efgh
ijkl

And Here's code :

void testUnsafe(void) {
    FILE *f;
    char buf[20];
    f = fopen("fil.txt", "r");
    while (!feof(f)) {
        fgets(buf, 20, f);
        if (buf[strlen(buf) - 1] == '\n') //cleaner
            buf[strlen(buf) - 1] = '\0';
        printf("%s , %d\n", buf, strlen(buf));
    }
    fclose(f);
}

void testSafe(void) {
    FILE *f;
    char buf[20];
    f = fopen("fil.txt", "r");
    while (fgets(buf, 20, f) != NULL) {
        if (buf[strlen(buf) - 1] == '\n') //cleaner
            buf[strlen(buf) - 1] = '\0';
        printf("%s , %d\n", buf, strlen(buf));
    }
    fclose(f);
}

Output is :

******unsafe test********
abcd , 4
efgh , 4
ijkl , 4
********safe test********
abcd , 4
efgh , 4
ijkl , 4

解决方案

If your text file ends without a newline after the last line of text, the testUnsafe() function will reach end-of-file when it reads the last line, and produce the three lines of output you have shown.

If your text file does have a newline after the last line of text, the function will read the last line, including the newline, without reaching end-of-file. When it enters the while() loop again, it reads zero characters, sets the end-of-file flag, and outputs the last line which is still in the buffer from the last round.

The while (!feof(f)) construction is not unsafe in itself. It's neglecting to check the return value of fgets() that is unsafe.

这篇关于从文件中读取和使用的feof退出循环()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆