Frage

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.

War es hilfreich?

Lösung

string data;

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

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

Andere Tipps

ifstream ReadFile;
ReadFile.open("input.txt");
string content;
string line;

 if (myReadFile.is_open()) {
     while (!ReadFile.eof()) {

        getline(cin,line);

        content += line + '\n';
        if (!line.empty()) {
            /// do what you want to do
        }
     }
  } 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top