lundi 21 janvier 2019

C++ loop with try/catch doesnt stop after "break"

so I wanna read a file into my vector of my own class with a loop, and in case of exception, catch and ignore them. The problem i keep on having is that it doesnt break when the input file is empty, i only have 9 rows of input but it still keeps running, and it wont print my vector after it either..

int main(int argc, const char* argv[])
{

    string fname = (argc > 1 ? argv[1] : "input.txt");
    ifstream ifs { fname };
    if (!ifs.is_open())
        throw runtime_error { "cannot open input file " + fname };

    vector < ClosedInterval > intervalle;
    int line = 0;
    for (ClosedInterval i;; ++line)
    {
        try
        {
            if (!(ifs >> i))
                break;

            intervalle.push_back(i);
        }
        catch (const exception& e)
        {
            cerr << "Error line " << line << ": " << e.what()
                    << "\n\tLine ignored\n";
        }
    }

    return EXIT_SUCCESS;
}

Aucun commentaire:

Enregistrer un commentaire