簡體   English   中英

使用 c++ 從文件中提取長行

[英]Extracting long lines from a file using c++

我正在嘗試從一個大文件(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

我在輸出文件中一無所獲。

代碼

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;
}

如果沒有 output,則可能是 getline() 失敗而沒有拋出異常(這可能嗎?),或者沒有找到 searchedStringtoBegin。

在此刻:

posbeginMEASUREMENT = current_line_in_file.find(searchedStringtoBegin,0);

似乎 find() 的返回值可能是 string::npos,因此數據輸出文件流中沒有 output 。

我希望這有助於回答這個問題!

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 作為輸入和 output 文件,而不是 fstream 。

我不認為您看到的問題是由排長隊引起的。

問題解決了! 謝謝大家的幫助 !

問題不在於行長

如果沒有檢測到searchedECU_ADDRESSsearchedECU_ADDRESS_EXTENSIONsearchedFORMAT ,我的代碼將停留在第一個while循環中

解決方案:添加條件檢測end MEASUREMENT

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;
        do
        {
            posEcuADDRESS = current_line_in_file.find(searchedECU_ADDRESS,0);
            posendMEASUREMENT = current_line_in_file.find(searchedStringtoEnd,0);
            if (posEcuADDRESS != string::npos)
            {
                ECU_ADDRESS = current_line_in_file;
                dataout << "Ecu address found:            " << ECU_ADDRESS.erase(0,18) << endl;
                break;
            }
            else 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'));
        
        do
        {
            posEcu_Address_Extension = current_line_in_file.find(searchedECU_ADDRESS_EXTENSION,0);
            posendMEASUREMENT = current_line_in_file.find(searchedStringtoEnd,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;
            }
            else if (posendMEASUREMENT != string::npos)
            {
                dataout << "\n" ;
                break;
            }
        } while (getline(datain, current_line_in_file, '\n'));

        do
        {
            posFORMAT = current_line_in_file.find(searchedFORMAT,0);
            posendMEASUREMENT = current_line_in_file.find(searchedStringtoEnd,0);
            if (posFORMAT != string::npos)
            {
                FORMAT = current_line_in_file;
                dataout << "Format of measurement found: " << FORMAT.erase(0,12) << "\n" << endl;
                break;
            }
            else if (posendMEASUREMENT != string::npos)
            {
                dataout << "\n" ;
                break;
            }
        } while (getline(datain, current_line_in_file, '\n'));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM