C ++忽略空的第一行 [英] C++ Ignore Empty First Line

查看:229
本文介绍了C ++忽略空的第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何忽略input.txt中的空白第一行?我不一定知道有一个空行(在这种情况下,但是我想使我的代码是通用的),所以我需要能够阅读行,如果有信息,或者跳过它,如果它是空白的。

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); ... }

我就是这样读的。如果我确实知道任何输入文件都是空的第一行,那我以前只会做getline,但是我不知道。

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.

推荐答案

string data;

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

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

这篇关于C ++忽略空的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆