如何在条件为c ++的情况下跳过下一行 [英] how to skip the next line if condition c++

查看:114
本文介绍了如何在条件为c ++的情况下跳过下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过其他示例,但我无法完成这一工作. 我有一个文件,该文件有时有一些奇怪的字符,例如:Äèîpg" 我不想保存该行,因为似乎getline的循环将在此处停止(它存储到带有垃圾箱的行).请问我该怎么办?我知道发生这种情况时:键= 0",下一行将包含这些字符Äèîpg".

I had a look in other examples but I can't make this one work. I have a file that at some point has weird characters for example : "Äèîpg" I don't want to save this line because it seems that the loop with getline will stop there (it stores until the line with the trash). How could I do it please? I know that when this occurs : "key =0", the next line will have these chars "Äèîpg".

这是我的代码:

    file = "example.log";
    ifstream f(file);
    f.open(file);
    if (f.good()){
        while (getline(f, line)) {
            lineNumber++;
            if ((lineNumber>= line1 - 20) && (lineNumber<= line2)){
                pos = line.find("key = 0");
                if (pos != string::npos){
                    std::cout << "skip the line" << endl;

                }
                else{
                    Type v;
                    v.line= line;
                    v.index = lineNumber;
                    linesVector.push_back(v);
                }
            }
        }
    } 
    f.close(); 

然后我用所需的信息创建一个剪切文件:

Then I create a cut file with the info I need:

    ofstream myfile;    
    string merge =  file + "_Cut.log";  
    myfile.open(merge);     
    for (unsigned int i = 0; i < linesVector.size(); i++){      
        myfile << linesVector[i].line << "\n";      
        //std::cout << linesVector[i].line << endl;     
    }   
    myfile.close();

在此先感谢您的帮助!

为弄清楚我的文件的外观,此处为原始的"example.log" (我无法将其连接到某处).

To make clear how my file looks like, here it the original "example.log" (I can't attach it somewhere).

2017-08-03 09:38:46 Expeum im6
2017-08-03 09:38:46 nubla4
2017-08-03 09:38:46 blaze
2017-08-03 09:38:46 ue
2017-08-03 09:38:46 er
2017-08-03 09:38:46 key = 0
2017-08-03 09:38:46 Q2žl2pE&ö³„Ôï¬ÈL+g…^cÎ1áø/7E›¸¥ü‰úLÎ’Æ
2017-08-03 09:38:46 81B9CEandrew499OEE4MUI5Q0VhbmRyZXc0OTk=
2017-08-03 09:38:47 B9CEandrew499OEE4MUI5Q0VhbmRyZXc0OTk=
2017-08-03 09:38:48 bla
2017-08-03 09:38:49 OK
2017-08-03 09:50:12  key = 0
2017-08-03 09:50:12 E&ö³„Ôï¬ÈL+g…^cÎ1áø/7E›¸¥ü‰úLÎ’Æ

这是剪切文件中的内容:

and here is what I get in the cut file :

2017-08-03 09:38:46 Expeum im6
2017-08-03 09:38:46 nubla4
2017-08-03 09:38:46 blaze
2017-08-03 09:38:46 ue
2017-08-03 09:38:46 er
2017-08-03 09:38:46 key = 0
2017-08-03 09:38:46 Q2žl2p

问题在于它卡在了最后的胡扯里,无法继续前进! 可能是缺少一些回车吗?我的想法不多了.

The problem is that it is stuck in the last giberrish and cannot move on ! Could it be some carriage return that is missing? I'm running out of ideas.

推荐答案

因此,您似乎想扔掉"key = 0"行和下一行?

So it looks like you're trying to throw away the line "key = 0" and the next line?

如果是这样,您只需在 ignore 中使用像这样嵌套if语句:f.ignore(numeric_limits<std::streamsize>::max(), '\n')

If so you can just use ignore in the nested if-statement like this: f.ignore(numeric_limits<std::streamsize>::max(), '\n')

如果lineNumber应该认为这是一行,则还需要添加++lineNumber.

If lineNumber should consider this a line you will also need to add ++lineNumber.

如果这可能是文件中的最后一行,则应该总共添加以下代码:

If this could be the last line in the file you should add this code in total:

if(!f.ignore(numeric_limits<std::streamsize>::max(), '\n')) {
    break;
}
++lineNumber;

实时示例

这篇关于如何在条件为c ++的情况下跳过下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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