how to skip the next line if condition c++

守給你的承諾、 提交于 2019-12-11 10:26:04

问题


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".

Here is my code:

    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();

Thank you in advance for your help !

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.


回答1:


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

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

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;

Live Example



来源:https://stackoverflow.com/questions/45576537/how-to-skip-the-next-line-if-condition-c

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