C++ Ignore Empty First Line

user1187621 picture user1187621 · Feb 14, 2012 · Viewed 16.1k times · Source

How do I ignore an empty first line in "input.txt"? I don't necessarily know that there is an empty line (in this particular case there is, but I want to make my code generic), so I need to be able to read the line if there is information, or skip it if it is blank. This is just for the first line.

while (getline(mcFile, line)) { 
    istringstream liness2(line); ... }

That's how I'm reading the lines. If I knew for certain that any input file I ran this on had an empty first line, I would just do "getline" before, but I don't know that.

Answer

Foggzie picture Foggzie · Feb 14, 2012
string data;

while (getline(inputFile, data))
{
    if (data == "") continue; // Skip blank line

    ... // Do stuff with non-blank line
}