我试图解析一个像下面的例子一样输出的文本文件, 我的例子有有限的条目, 但我的实际文件有超过15000行, 所以我无法单独读取这些 :
ID IC 时
15:23:43.867/g/mydata/dataoutputfile.txt 识别符号
0003 1233 abcd
00 43 eb54 abf3
000f 0bb4 ac24
000 a325 ac75
0023 0043 ac91 15:23:44.000 /g/mydata/dataoutputfile.txt identifier
0003 1233 abcd
00 43 eb54 abf3
000f 0bb4 ac24
000 a325 ac75
0023 0043 ac91
是一种我拥有的输出。 时间列经常重置 。
我现在正在做的是,除了我的例子中的3个栏外,再增加2个栏。第一栏是将ID栏转换为可理解的信息。第二栏将计算每个时间代码之间的差异,但时间代码重置除外。
我的逻辑是,把每一列都读成一个阵列 这样我就能完成必要的翻译和操作
我的重点是先拿到时间代码差数, 因为我认为得到翻译会比较简单。
我的问题是把条目读入他们的矩阵表:
我的代码看起来有点像这个
while(readOK && getline(myfile,line))
{
stringstream ss(line);
string ident,IC,timehex,time,filelocation;
string junk1,junk2;
int ID[count];
int timecode[count2];
int idx=0;
if(line.find("ID") !=string::npos)
{
readOK=ss>>ident>>IC>>timehex;
myfile2<<ident<<" "<<IC<<" "<<timehex<<" "<<"ID Decoded"<<" "<<"DT"<<endl;
myfile3<<"headers read"<<endl
}
else if(line.find("identifier") != string::npos)
{
readOK=ss>>time>>filelocation;
myfile3<<"time and location read";
myfile2<<time<<" "<<filelocation<<endl;
}
else //this is for the hex code lines
{
readOK=ss>>hex>>ID[idx]>>IC>>timecode[idx];
if (readOK)
{
myfile2<<setw(4)<<setfill( 0 )<<hex<<ID[1000]<<" "<<IC<<" "<<timecode[1000]<<endl;
myfile3<<"success reading info into arrays"<<endl;
}
else
myfile3<<"error reading hex codes"<<endl;
}
idx++;
}
虽然这个代码工作不正确, 我无法在每行都读一样的字, 因为插入了中间的时间和文件位置条目, 帮助记录我读到代码时的状态 。
我的直觉告诉我,我打给矩阵条目的时间太早了,它们还没有被填满,因为如果我用1000计数,我就会得到一个0(我的输入文件中有超过15000行,我在我的程序的另一部分动态地设置了阵列的界限)。
我似乎无法找出如何正确分配条目, 因为我有些继承问题, 数数变量每次在循环中调整为0。