Read a file twice in C++ because of eof?

做~自己de王妃 提交于 2019-12-25 10:16:33

问题


I read numbers from a file, apply 3 functions and print out to another file:

int main(int argc, char** argv) {
    std::ifstream fin;
    fin.open("input.txt");

    std::ofstream fout;
    fout.open("output.txt", std::ios::app);

    char arr[50];
    int a,b;
    int N;//number to factor

    while (!fin.eof()){
        //Print backward
        fin >> arr;
        PrintBackward( arr );
        fout << endl;

        //Greatest common divisor
        ((fin >> a) >> b);
        fout << gcd( a, b );
        fout << endl;

        //Find prime factor
        fin >> N;
        PrimeFactor(N);
        fout << endl;     
    }

    fin.close();
    fout.close();

    return 0;
}

After running, the result is duplicated:

olleh
3
2 3 7 
olleh
3
2 3 7

I read a similar article but it's about reading into 1 variable so it seems not to be feasible.

If I set a break at the end of the while loop, it's fine. Is there any way not to use break?


回答1:


while (!whatever.eof()) is essentially always wrong, and will never detect the end of the file correctly. In your case, it's easiest to coalesce the reads together, and then do all the processing, something like this:

while (fin >> arr >> a >> b >> N) {
    PrintBackwards(arr);
    fout << "\n";

    fout << gcd(a, b) << "\n";

    fout << PrimeFactor(N) << "\n";
}

The crucial part is to check the result of the read, instead of checking and reading separately from each other.

A couple more bits of advice: I'd use an std::string instead of an array. I'd also separate reversing the string from printing it, so you can have something like:

fout << reverse(arr)   << "\n" 
     << gcd(a, b)      << "\n" 
     << PrimeFactor(N) << "\n";

Emphasizing the commonality between the operations tends to be a good thing.

Edit: Just for fun, I'll point out another way you could do things if you wanted. Since you're basically reading and processing the four items as a group, you could make that grouping a bit more explicit:

struct item { 
     std::string arr;
     int a, b, N;

     friend std::istream &operator>>(std::istream &is, item &i) { 
         return is >> arr >> a >> b >> N;
     }
};

struct process {
    std::string operator()(item const &i) { 
        std::ostringstream buffer;

        buffer << reverse(arr) << "\n" << gcd(a, b) << "\n" << PrimeFactor(N);
        return buffer.str();
    }
}

With this, you can let the standard library deal with all the details of the reading and writing, checking end of file, etc.:

std::transform(std::istream_iterator<item>(fin), 
               std::istream_iterator<item>(),
               std::ostream_iterator<std::string>(std::cout, "\n"),
               process());



回答2:


My guess is that you're checking eof too early - it's only set when you try to read and the read fails because you're at the end of the file. Try adding this after fin >> arr:

if (fin.eof()) break;

Actually you should be checking for errors after every IO operation - not to do so is sloppy coding and won't be robust.



来源:https://stackoverflow.com/questions/5684537/read-a-file-twice-in-c-because-of-eof

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!