如果条件 c++ 如何跳过下一行
how to skip the next line if condition c++
我查看了其他示例,但我做不到这个。
我有一个文件,在某些时候有奇怪的字符,例如:“Äèîpg”
我不想保存这一行,因为看起来带有 getline 的循环将停在那里(它存储到有垃圾的行)。请问我该怎么做?我知道发生这种情况时:"key =0",下一行将包含这些字符“Äèî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();
然后我用我需要的信息创建一个剪切文件:
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"
(我无法将其附加到某处)。
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Î’Æ
这是我在剪切文件中得到的:
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
问题是卡在了最后一个乱码,无法继续前进!
会不会是少了一些马车return?我运行没主意了。
看来您是想删除第 "key = 0" 行和下一行?
如果是这样,您可以在嵌套的 if
语句中使用 ignore
,如下所示:f.ignore(numeric_limits<std::streamsize>::max(), '\n')
如果 lineNumber
认为这是一行,您还需要添加 ++lineNumber
.
如果这可能是文件中的最后一行,您应该总共添加以下代码:
if(!f.ignore(numeric_limits<std::streamsize>::max(), '\n')) {
break;
}
++lineNumber;
我查看了其他示例,但我做不到这个。 我有一个文件,在某些时候有奇怪的字符,例如:“Äèîpg” 我不想保存这一行,因为看起来带有 getline 的循环将停在那里(它存储到有垃圾的行)。请问我该怎么做?我知道发生这种情况时:"key =0",下一行将包含这些字符“Äèî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();
然后我用我需要的信息创建一个剪切文件:
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" (我无法将其附加到某处)。
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Î’Æ
这是我在剪切文件中得到的:
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
问题是卡在了最后一个乱码,无法继续前进! 会不会是少了一些马车return?我运行没主意了。
看来您是想删除第 "key = 0" 行和下一行?
如果是这样,您可以在嵌套的 if
语句中使用 ignore
,如下所示:f.ignore(numeric_limits<std::streamsize>::max(), '\n')
如果 lineNumber
认为这是一行,您还需要添加 ++lineNumber
.
如果这可能是文件中的最后一行,您应该总共添加以下代码:
if(!f.ignore(numeric_limits<std::streamsize>::max(), '\n')) {
break;
}
++lineNumber;