本文介绍了DEV-C ++ eof()决定不起作用.没有意义.请帮忙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dec-cpp,因此代码("result.txt"存在)永远持续下去.它不应该.请帮忙.

I use dec-cpp and the code as such ("result.txt" being present) continues on forever. It should not. Please help.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
      ifstream fin_sort("result.txt");
     
     char txt[2]="";
     while(!fin_sort.eof()){
                              fin_sort.getline(txt,2);
                              cout << " -" << txt << "- " << endl;
                              
                              }
     fin_sort.close();
 
 return 0;   
}

推荐答案


ex
am
pl
e
example
ex
am
pl
e



输出:



output:

ex
am
pl
e










...



这是一团糟.有人可以向我解释吗?
只是我想了解.

谢谢



This is one big mess. Can anyone explain this to me?
It''s just that I would like to understand.

Thanks


#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream fin_sort("result.txt");
    char txt[3] = ""; //Allow space for 2 characters and a NULL terminator
    while (!fin_sort.eof()) {
        fin_sort.getline(txt, sizeof(txt)); //It is usually better to use sizeof() in case you change the size later
        cout << " -" << txt << "- " << endl;
        if (fin_sort.fail()) {
            cout << "Fail bit set. Clearing it for read to resume" << endl;
            fin_sort.clear(); //Clear the fail bit (Warning: this function can also clear the EOF bit if not used properley)
        }
    }
    fin_sort.close();
    return 0;
}


这篇关于DEV-C ++ eof()决定不起作用.没有意义.请帮忙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:41