这是我的代码,当我读取必须存储在类中的行,它读取一行并存储下一行的值时,我无法识别代码中的错误。请帮我纠正一下。任务是跳过包含句子的行,并读取数据abd将其存储在类变量中的行。
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;
class data_Read
{
public:
/* timestamp */
double time;
/*state of the signal 1 or 2*/
unsigned int state;
/*ID in hexadecimal and converted to decimal*/
std::string ID;
/* Received or transmitted message */
std::string status;
/* Message type */
std::string type;
/* byte length */
unsigned int byte_lent;
/* CAN Message */
std::string message_1;
std::string message_2;
std::string message_3;
std::string message_4;
std::string message_5;
std::string message_6;
std::string message_7;
std::string message_8;
std::string type1;
std::string type2;
std::string type3;
std::string type4;
std::string type5;
std::string type6;
std::string type7;
std::string type8;
std::string type9;
};
int main()
{
std::ifstream ifs("datatrace.txt");
data_Read data;
std::string line;
while (!ifs.eof())
{
start:
std::getline(ifs, line);
std::cout << line << std::endl;
if (!isspace(line[0]))
{
goto start;
}
std::size_t space = line.find(" ", 0);
if (space == 0)
{
int i;
for (i = 0; i <= 15; i++)
{
if (isalpha(line[i]))
{
goto start;
}
}
ifs >> data.time >> data.state >> data.ID >> data.status >> data.type >> data.byte_lent >> data.CAN_message_1 >> data.CAN_message_2 >> data.CAN_message_3 >> data.CAN_message_4 >> data.CAN_message_5 >> data.CAN_message_6;
ifs >> data.CAN_message_7 >> data.CAN_message_8 >> data.type1 >> data.type2 >> data.type3 >> data.type4 >> data.type5 >> data.type6 >> data.type7>> data.type8 >> data.type9;
cout << data.time << "\t" << data.state << "\t" << data.ID << "\t" << data.status << "\t" << data.type << "\t" << data.byte_lent << "\t" << data.CAN_message_1 << "\t" << data.CAN_message_2 << "\t" << data.CAN_message_3 << "\t";
cout << data.CAN_message_4 << "\t"<< data.CAN_message_5 << "\t"<< data.CAN_message_6 << "\t" << data.CAN_message_7 << "\t"<< data.CAN_message_8 << "\t"<< data.type1 << "\t"<< data.type2 << "\t"<< data.type3 << "\t"<< data.type4 << "\t"<< data.type5 << "\t" << data.type6 << "\t"<< data.type7 << "\t"<< data.type8 << "\t"<< data.type9 << endl;
}
}
ifs.close();
return 0;
}
,文本文件包含
date Fri Sep 1 02:11:40.195 pm 2017
base hex timestamps absolute
internal events logged
// version 9.0.0
Begin Triggerblock Fri Sep 1 02:11:40.195 pm 2017
0.000000 Start of measurement
0.002893 1 201 Rx d 8 06 0D 00 B0 89 00 0D E7 Length = 227925 BitCount = 118 ID = 513
0.003133 1 280 Rx d 8 1B 0C 7C F1 E8 75 39 67 Length = 221910 BitCount = 115 ID = 640
0.006981 CAN 1 Status:chip status error active
0.006981 CAN 2 Status:chip status error active
0.007123 1 244 Rx d 8 7B 01 00 08 80 80 C0 00 Length = 233925 BitCount = 121 ID = 580
0.007148 2 B2 Rx d 8 C0 13 9A 13 D8 13 C0 13 Length = 221910 BitCount = 115 ID = 178
0.007359 1 246 Rx d 8 55 01 00 49 50 B6 7A 89 Length = 217925 BitCount = 113 ID = 582
0.007394 2 86 Rx d 8 62 00 2A 20 01 84 02 00 Length = 225925 BitCount = 117 ID = 134
答案 0 :(得分:0)
xx.xx