如何解析以下文件
how to parse the following file
我在将一些python代码传输到C++时遇到了一个小问题我有一个格式像的文件
============================== There are 0 element
============================== There are 62 element
1VTZ0 13 196 196 13 196 196 184 2520 2BUKA 1 1VTZ0 1 195
1VTZ1 13 196 196 13 196 196 184 2520 2BUKA 1 1VTZ1 1 195
1VTZ2 13 196 196 13 196 196 184 2350 2BUKA 1 1VTZ2 1 195
1VTZ3 13 196 196 13 196 196 184 2470 2BUKA 1 1VTZ3 1 195
1VTZ4 13 196 196 13 196 196 184 2560 2BUKA 1 1VTZ4 1 195
1VTZ5 13 196 196 13 196 196 184 2340 2BUKA 1 1VTZ5 1 195
1VTZ6 13 196 196 13 196 196 184 3320 2BUKA 1 1VTZ6 1 195
1VTZ7 13 196 196 13 196 196 184 2820 2BUKA 1 1VTZ7 1 195
1VTZe 13 196 196 13 196 196 184 2140 2BUKA 1 1VTZe 1 195
我想做的是跳过行===================== There are 0 element
。在我的原始python代码中,我只需要使用if条件:
if (not "=====" in line2) and (len(line2)>30):
我在C++中使用了类似的if条件,但仍然无法摆脱"元素"行,我的C++代码如下:
unsigned pos1, pos2;
string needle1="element"
string needle2="====="
Infile.open(filename.c_str(), ios::in);
while (!Infile.eof())
{
if(line==" ")
{
cout<<"end of the file" <<endl;
break;
}
getline(Infile, line);
pos1=line.find(needle1);
if (pos1!=string::npos&& pos2!=string::npos && (line.length()> 40))
{
...........
}
谢谢!
我不知道为什么要检查行长度,因为有更好的方法可以做到这一点。
例如,您可以对字符串进行索引(因为它是一个字符数组),然后在while循环中使用continue
语句。
while (!Infile.eof()){
if (line[0] == '=') { // this assumes that no other lines start with '='
continue;
}
if(line==" "){
cout<<"end of the file" <<endl;
break;
}
// Do whatever you need to do with the non "===... XX element" lines
}
http://www.cplusplus.com/reference/string/string/operator[]/http://www.learncpp.com/cpp-tutorial/58-break-and-continue/
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- Cppcheck生成xml转储文件
- 读取文件的最后一行并输入到链接列表时出错
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- 如何将内容数组写入文本文件?
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到