33,311
社区成员
发帖
与我相关
我的任务
分享
while(p)
{
int k = atoi(p);
if(k + 1 > table.size()) table.resize(k + 1);//先判断是否越界,否则增大table的size。。
table[k].read.push_back(count);
p = strtok(NULL,",");
}
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
typedef struct transaction
{
vector<int>read;
}tran;
int main()
{
vector<tran> table;
int count=0;
char s[1000];
ifstream infile;
tran t;
char *p;
infile.open("C:\\Users\\liwei\\Desktop\\MATI\\T2.txt");
if(infile==NULL)
{
cout<<"File is not exist"<<endl;
exit(0);
}
else if(infile.is_open())
{
cout<<"File open succeed."<<endl;
while(infile.getline(s,1000,'\n'))
{
p = strtok(s,",");
while(p)
{
// table[atoi(p)].read.push_back(count);//这样的结构很吓人,呵呵
t.read.push_back(atoi(p));
p = strtok(NULL,",");
}
table.push_back(t);
t.read.clear();
}
}
int i,j;
i=j=0;
for(i=0;i<table.size();i++){
for(j=0;j<table[i].read.size();j++){
cout<<table[i].read[j]<<" ";
}
cout<<"第"<<i<<"行"<<endl;
}
infile.close();
}