• 1
name Punditsdkoslkdosdkoskdo

Is it possible in C++ ifstream and getline?

I'm using an old gcc compiler if that matters (before C++11) and have a function with the code below .

Just want to make sure: 1. I do not need to check if the file actually exists right, getline knows to handle this ? 2.There is no need to close the stream in the end because of RAII ?

ifstream in(file);
string line;
while (std::getline(in, line))


You can see the example given in the C++ reference website here.

There are explanations for different versions of it, like C++98 and C++11.

As for answers:

  1. getline() will fail, and your code won't get into the while loop, if it fails to open the file beforehand.

  2. As you said, you don't need to close the file because of RAII.

  • 1
Reply Report