vendredi 26 mai 2017

How to best deal with empty lines when overloading the input stream operator?

I'd like to read a file line by line, and for every non-empty file create a new instance of some class Foo. I'm unsure how to best achieve that.

So far, I have overloaded the >> operator for Foo:

std::istream& operator>>(std::istream& is, Foo& foo)
{
    std::string line;
    std::getline(is,line);

    if (!line.empty()){
        foo.valid = true; 
    }
    return is;
}

Now I can do the following:

std::ifstream ifs(file);
Foo *foo = new Foo();
std::vector<Foo> all_foos;    

while(ifs >> *foo) {
    if(foo.valid) { 
       all_foos.push_back(foo);
       foo = new Foo();
    } else {
       delete foo;
    }
}
ifs.close();

I think that this is rather ugly and there has to be a nicer solution that doesn't require the valid flag. But how? As far as I can see, the >> operator requires an instance of Foo - but how does one deal with this instance in case it's not needed, e.g., when the input stream reaches EOF or contains an empty line?

What's the best way to do this in C++?

Aucun commentaire:

Enregistrer un commentaire