问题
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.
回答1:
string data;
while (getline(inputFile, data))
{
if (data == "") continue; // Skip blank line
... // Do stuff with non-blank line
}
回答2:
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
}
}
}
来源:https://stackoverflow.com/questions/9281958/c-ignore-empty-first-line