修改代码
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
using namespace std;
struct PART
{
string id;
string seq;
vector<string> andl;
};
struct PART info;
struct HEAD
{
vector<PART> Info;
};
struct HEAD head;
int main()
{
string str, src;
string filename = "456.txt";
// string filename = "openroad.txt";
ifstream infile(filename.c_str());
while(getline(infile,str))
{
string::size_type m = str.find('\t');
src = str.substr(0,m);
if( src == "@part")
head.Info.push_back(info);
if( src == "@l" )
{
info.andl.push_back(str.substr(m));
}
if( src == "@seq" )
{
info.seq = str.substr(m);
}
if( src == "@id" )
{
info.id = str.substr(m);
}
}
for(vector<PART>::iterator it = head.Info.begin()+1; it != head.Info.end(); ++it)
{
cout<<"@part"<<endl;
cout<<"@id"<<it->id<<endl;
cout<<"@seq"<<it->seq<<endl;
for(vector<string>::iterator is = (*it).andl.begin(); is != (*it).andl.end(); ++is )
cout<<"@l"<<*is<<endl;
cout<<endl;
}
return 0;
}
这个输出的是
@part
@id 2
@seq 17
@l 553720 5434138 6378397 1
@part
@id 2
@seq 18
@l 553720 5434138 6378397 1
@l 553720 17567680 17567697 1
@l 553730 17567674 17567686 1
@l 553720 17567697 17567674 1
@part
@id 2
@seq 19
@l 553720 5434138 6378397 1
@l 553720 17567680 17567697 1
@l 553730 17567674 17567686 1
@l 553720 17567697 17567674 1
@l 553730 17567686 17567672 1
@part
@id 2
@seq 20
@l 553720 5434138 6378397 1
@l 553720 17567680 17567697 1
@l 553730 17567674 17567686 1
@l 553720 17567697 17567674 1
@l 553730 17567686 17567672 1
@l 553627 17567696 16591773 1
@l 553627 17567667 17567696 1
我要的是这种
@part
@id 2
@seq 17
@l 553720 5434138 6378397 1
@part
@id 2
@seq 18
@l 553720 17567680 17567697 1
@l 553730 17567674 17567686 1
@l 553720 17567697 17567674 1
@part
@id 2
@seq 19
@l 553730 17567686 17567672 1
@part
@id 2
@seq 20
@l 553627 17567696 16591773 1
@l 553627 17567667 17567696 1
帮我改一下 怎么输出这个
[解决办法]
- C/C++ code
struct PART{ string id; string seq; vector<string> andl; int nCount;//加个计数的}; while(getline(infile,str))//读取的代码改下 { string::size_type m = str.find('\t'); src = str.substr(0,m); if( src == "@part") { head.Info.push_back(info); } 