我试图从一个大文件(A2L文件400 MO)中提取数据到一个输出文件,但是我的代码的问题是当它们超过一定长度时不接受行。
从这部分:
/begin MEASUREMENT VX106x.mon.te.tfifo.extRamMaxByte "Maximum fill level (for each monitor event cycle) of the external trace memory on the Basemodule"
ULONG NO_COMPU_METHOD 0 0 0 4294967295
BYTE_ORDER MSB_LAST
ECU_ADDRESS 0x91000248
ECU_ADDRESS_EXTENSION 0xFD
FORMAT "%.15"
/begin IF_DATA XCP
/begin DAQ_EVENT VARIABLE
/begin AVAILABLE_EVENT_LIST /end AVAILABLE_EVENT_LIST
/begin DEFAULT_EVENT_LIST
EVENT 0x8000
/end DEFAULT_EVENT_LIST
/end DAQ_EVENT
/end IF_DATA
/end MEASUREMENT
我的代码给了我这样的信息:
Measurement: VX106x.mon.te.tfifo.extRamMaxByte "Maximum fill level (for each monitor event cycle) of the external trace memory on the Basemodule"
Ecu address found: 0x91000248
Ecu address extention found: 0xFD
Format of measurement found: "%.15"
但是当数据行是这种形式时(行很长):
/begin MEASUREMENT _g_PER_Hil_PerSppRObjHilInputRunnable_PerSppRObjHilInputRunnable_m_radarSensorPropertiesPort_out_local.TChangeableMemPool._._._m_arrayPool._0_._elem._m_collection._m_collection._m_memory._m_values._7_.InputCartesianObject._._m_state._m_vectorCovariancePair._m_muVector._m_data._m_data._m_value._0_ ""
FLOAT32_IEEE NO_COMPU_METHOD 0 0 -3.40282346639E+38 3.40282346639E+38
BYTE_ORDER MSB_LAST
ECU_ADDRESS 0xB0041270
SYMBOL_LINK "_g_PER_Hil_PerSppRObjHilInputRunnable_PerSppRObjHilInputRunnable_m_radarSensorPropertiesPort_out_local.TChangeableMemPool._._._m_arrayPool._0_._elem._m_collection._m_collection._m_memory._m_values._7_.InputCartesianObject._._m_state._m_vectorCovariancePair._m_muVector._m_data._m_data._m_value._0_" 0
/end MEASUREMENT
我在outuput文件中一无所获。
代码
int main() {
string searchedStringtoBegin = "begin MEASUREMENT";
string searchedStringtoEnd = "end MEASUREMENT";
string searchedECU_ADDRESS = "ECU_ADDRESS";
string searchedECU_ADDRESS_EXTENSION = "ECU_ADDRESS_EXTENSION";
string searchedFORMAT = "FORMAT";
string outputFile="A2loutputFile.a2l";
fstream datain, dataout;
string current_line_in_file;
string::size_type posbeginMEASUREMENT, posEcuADDRESS, posEcu_Address_Extension, posFORMAT;
time_t start, end;
double time_taken;
string MEASUREMENT_NAME, ECU_ADDRESS, ECU_ADDRESS_EXTENSION, FORMAT;
cout << "Reading from an A2L file \n";
time(&start);
datain.open("examplea2l.a2l",fstream::in);
dataout.open("A2LoutputFile.a2l",fstream::out);
while (getline(datain, current_line_in_file,'\n'))
{
posbeginMEASUREMENT = current_line_in_file.find(searchedStringtoBegin,0);
if (posbeginMEASUREMENT != string::npos)
{
dataout << "Measurement: " << current_line_in_file.erase(0,23) <<endl;
while (getline(datain, current_line_in_file, '\n'))
{
posEcuADDRESS = current_line_in_file.find(searchedECU_ADDRESS,0);
if (posEcuADDRESS != string::npos)
{
ECU_ADDRESS = current_line_in_file;
dataout << "Ecu address found: " << ECU_ADDRESS.erase(0,18) << endl;
break;
}
}
while (getline(datain, current_line_in_file, '\n'))
{
posEcu_Address_Extension = current_line_in_file.find(searchedECU_ADDRESS_EXTENSION,0);
if (posEcu_Address_Extension != string::npos)
{
ECU_ADDRESS_EXTENSION = current_line_in_file;
dataout << "Ecu address extention found: " << ECU_ADDRESS_EXTENSION.erase(0,27) << endl;
break;
}
}
while (getline(datain, current_line_in_file, '\n'))
{
posFORMAT = current_line_in_file.find(searchedFORMAT,0);
if (posFORMAT != string::npos)
{
FORMAT = current_line_in_file;
dataout << "Format of measurement found: " << FORMAT.erase(0,12) << "\n" << endl;
break;
}
}
}
}
// 739369
datain.close();
dataout.close();
time(&end);
time_taken = end - start;
cout << "Time of execution: " << fixed << time_taken << setprecision(3) << " seconds" << endl;
return 0;
}
如果没有输出,可能是getline()失败而没有引发异常(这可能吗?),或者找不到searchedStringtoBegin。
此时:
posbeginMEASUREMENT = current_line_in_file.find(searchedStringtoBegin,0);
find()的返回值可能是string::npos,因此没有任何东西输出到dataout文件流。
我希望这有助于回答这个问题!
string outputFile="A2loutputFile.a2l";
dataout.open("A2LoutputFile.a2l",fstream::out);
请注意,您没有将outputFile
名称传递给fstream(您可以使用outputFile.c_str()
)。 您传递的实际文件名在A2L
中使用大写的L
。 也许你看错文件了。 您还应该检查dataout.good()
以确认文件确实已创建。
第二个文件没有扩展名和格式。 您可能需要添加这些内容,然后重试。
我不确定这是否是堆栈溢出的格式问题,但我在第一个文件中的第一个单词之前计算了6个空格,而在第二个文件中有5个空格。 看来您硬编码了current_line_in_file.erase(0,23)
的空格数。 也许你更喜欢在文件中寻找,直到找到你要找的东西。
我将使用ifstream和ofstream作为输入和输出文件,而不是同时使用fstream。
我不认为你看到的问题是由排长队引起的。