0

istream eof の使用は「バグ」であり、コードを記述する「正式な」方法ではないことを読んだので、使用するより良いコードは何ですか? たとえば、次のコードがあります。

using namespace std; //I've heard this is bad practice also

int main(){
    string line;
    ifstream myfile("example.txt");
    while(!myfile.eof()){
        getline(myfile, line);
    }//while
    //do something with line
}//main

!myfile.eof() を何に置き換える必要がありますか? ありがとう!

4

2 に答える 2

3
if (ifstream myfile("example.txt"))
{
    while (getline(myfile, line))
    {
        ...
    }
    if (!eof(myfile))
        std::cerr << "error before end of input file\n";
}
else
    std::cerr << "error opening input file\n";
于 2013-04-23T02:15:57.433 に答える