首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++解析文件解决办法

2013-01-11 
c++解析文件有下面一种格式文件test.txt:bbbhello2012-12-20 11:26:18eeebbbp1aaaap2ddddp3vvvv

c++解析文件
有下面一种格式文件test.txt:
<bbb>
hello  =  2012-12-20 11:26:18
<eee>
<bbb>
p1  =  aaaa
p2  =  dddd
p3  =  vvvvv
<eee>
<bbb>
p1  =  bbbb
p2  =  fffff
p3  =  nnnnn
<eee>
....
<bbb>
hello  =  2012-12-20 11:26:18
<eee>

要解析文件,如果是hello的跳过;
其它的要解析出每组的p1,p2,p3各自的值,按行输出到一个文件,即每组一行输出。
c++如何实现?
[解决办法]
我不会正则,但我会不用正则写
我是新手一个,代码不优美,别介意。
以下调试正确!


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
cout << "请输入你想打开的文件名:" << endl;
ifstream inFile;
string fileName;
cin >> fileName;
if (!cin){
cout << "你输入有误,程序结束!" << endl;
return -1;
}
else
inFile.open(fileName);
cout << "下面为你显示该文件中相应p的值:" << endl;
int ix(1);
while (inFile)
{
string pstr;
getline(inFile, pstr);
if (pstr.size()!=0 && pstr[0] == 'p')
{
cout << "第" << ix << "组:\t" << pstr << "\t";
int n(2);
while (n != 0)
{
string npstr;
getline(inFile, npstr);
cout << npstr << "\t";
if (npstr[1] == '3')
cout << endl;
--n;
}
++ix;
}
}
inFile.close();
return 0;
}

热点排行