Think of this as an extended comment to Nawaz' already excellent answer.
Regarding your first option,
while (fs.good()) {
std::string line;
std::getline(fs, line);
...
This has multiple problems. Problem number 1 as that that the while
condition is in the wrong place and is superfluous. It's in the wrong place because fs.good()
indicates whether or not the most recent action performed on the file was OK. A while condition should be with respect to the upcoming actions, not the previous ones. There is no way to know whether the upcoming action on the file will be OK. What upcoming action? fs.good()
does not read your code to see what that upcoming action is.
Problem number two is that the you are ignoring the return status from std::getline()
. That's OK if you immediately check the status with fs.good()
. So, fixing this up a bit,
while (true) {
std::string line;
if (std::getline(fs, line)) {
...
}
else {
break;
}
}
Alternatively, you can do if (! std::getline(fs, line)) { break; }
but now you have a break
in the middle of the loop. Yech. It is much, much better to make the exit conditions a part of the loop statement itself if at all possible.
Compare that to
std::string line;
while (std::getline(fs, line)) {
...
}
This is the standard idiom for reading lines from a file. A very similar idiom exists in C. This idiom is very old, very widely used, and very widely viewed as the correct way to read lines from a file.
What if you come from a shop that bans conditionals with side-effects? (There are lots and lots of programming standards that do just that.) There is a way around this without resorting to the break in the middle of the loop approach:
std::string line;
for (std::getline(fs, line); fs.good(); std::getline(fs, line)) {
...
}
Not as ugly as the break approach, but most will agree that this isn't nearly as nice-looking as is the standard idiom.
My recommendation is to use the standard idiom unless some standards idiot has banned its use.
Addendum
Regarding for (std::getline(fs, line); fs.good(); std::getline(fs, line))
: This is ugly for two reasons. One is that obvious chunk of replicated code.
Less obvious is that calling getline
and then good
breaks atomicity. What if some other thread is also reading from the file? This isn't quite so important right now because C++ I/O currently is not threadsafe. It will be in the upcoming C++11. Breaking atomicity just to keep the enforcers of the standards happy is recipe for disaster.