C++ Ignore Empty First Line

随声附和 提交于 2020-01-04 05:26:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!