vendredi 22 décembre 2017

Why is this line infinite loop?

The following code for reading a file worked and completed instantly.

auto copy = [&](const FileName &src, std::ofstream &dst)
{
    std::ifstream r(src);
    std::string line;
    while (std::getline(r, line)) { dst << line << std::endl; }
};

src is a small file, dst is a working output stream. If I changed the loop to:

while (std::getline(std::ifstream(src), line)) { dst << line << std::endl; }

My program was running forever in the while loop. Why infinite looping? I'm using Xcode C++11.

Aucun commentaire:

Enregistrer un commentaire