提取特定字符之间的字符串

来源:百度知道 编辑:UC知道 时间:2024/07/03 09:09:33
a.txt里面内容如下:
啊富利卡就就
啊两地分居阿里
<n>张三</n>
阿伦多夫
阿弗莱等级分类
<a>湖北</a>
adlljd
jaldfj
<n>李四</n>
kjlrgjkl;s
jksdflgksfl
<a>湖南</a>
发生开发;jfdlajd
……
……

现要求提取<n></n>和<a></a>之间的内容保存到b.txt
即b.txt里面的内容应该为:
张三
湖北
李四
湖南
……
……

//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main(void)
{
ifstream inf("f:\\a.txt"); /*原文件*/
ofstream otf("f:\\b.txt"); /*目标文件*/
string line;
if (!inf) {
cout<<"ERROR-file not found!"<<endl;
return 1;
}

while (inf>>line)
{
string::size_type b,e;
if ((b=line.find("<a>"))!=string::npos ) {
e=line.find("</a>");
cout<<line.substr(b+3,e-b-3)<<endl;
otf<< line.substr(b+3,e-b-3)<<endl;
}
else if ((b=line.find("<n>"))!=string::npos) {
e=line.find("</n>");
cout<<line.substr(b+3,e-b-3)<<endl;
otf<<line.sub