如果条件 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();
提前感谢您的帮助!
为了弄清楚我的文件是什么样子的,这里是原始的"示例.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
问题是它被困在最后的吉伯里什,无法继续前进! 会不会是缺少了一些回车?我的想法快用完了。
所以看起来你试图扔掉"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;
现场示例
相关文章:
- 无论我使用什么,我的输入都会被跳过
- C++ If/Else 语句被跳过
- 为什么调试器总是跳过此程序中的一行?
- 如果条件 C++,如何跳过下一行
- C++ : 如何在使用 getline() 和 ifstream 对象从文件中读取一行时跳过第一个空格?
- C++如何跳过一行?
- C :使用getline从文本文件中输入,要么跳过第一行,要么将其余部分弄乱
- 循环每隔一行跳过一次
- 为什么在同一行上有 2 个函数调用时跳过函数调用
- getline() 跳过第一行和最后一行之间每一行的第一列
- 简单的C++代码跳过文本文件中的一行
- C++ifstream跳过第一行
- 跳过csv文件的第一行
- 为什么它跳过第一行
- 如何从文件中读取一行文本而不跳过空行?
- Getline跳过while循环中的第一行??(c++)
- 从文件中读取和存储数据时,每隔一行数据就会被跳过
- 打开文件;但是要跳过每一行
- 从文件中读取时,如何跳过数组的第一行
- Gnuplot:跳过最后一行/不完整的数据文件