簡體   English   中英

從文件 C++ 中提取數字

[英]Extracting number from file C++

在我的程序中,我想從一行中提取一個數字(代表以秒為單位的停止時間),從中我減少我從下一行提取的另一個數字(代表以秒為單位的開始時間),然后寫入結果在結果文件中。

我有以下代碼:

# include <iostream>
# include <fstream>
# include <string>
# include <streambuf>

using namespace std;

int main ()
{

    ifstream f;
    ofstream g;
    f.open("Processes.txt");
    g.open("Results.txt");

    long last_el,top_nr=0,stop_nr=0,start_nr=0,stop_time_count=1,start_time_count=1,inter_count=0,global_count,final_result_count=1;
    long pow_start_nr=7,pow_stop_nr=7,stop_time[1000],start_time[1000],final_result[1000];
    string text_el;

    stringstream strStream;
    strStream << f.rdbuf();
    string str = strStream.str();
    
    last_el=text_el.size();
    
   // while(text.el)
   // {
       for(global_count=0;global_count<last_el;global_count++)
       {
          if(text_el[global_count]=='*')
          {
             inter_count=global_count+1;
             for(global_count=inter_count;global_count<=inter_count+7;global_count++)
             {
                start_nr=start_nr+(10^pow_start_nr)*text_el[global_count];
                pow_start_nr--;
             }
             start_time[start_time_count]=start_nr;
             start_time_count++;
             global_count=inter_count+8;
          }
       }

       for(global_count=0;global_count<last_el;global_count++)
       {
          if(text_el[global_count]=='#')
          {
             inter_count=global_count+1;
             for(global_count=inter_count;global_count<=inter_count+7;global_count++)
             {
                stop_nr=stop_nr+(10^pow_stop_nr)*text_el[global_count];
                pow_stop_nr--;
             }
                stop_time[stop_time_count]=stop_nr;
                stop_time_count++;
                global_count=inter_count+8;
             }
          }
  //  }

    for(final_result_count=1;final_result_count<2;final_result_count++)
    {
       final_result[final_result_count]=stop_time[stop_time_count]-start_time[start_time_count];
       stop_time_count++;
       start_time_count++;
       g<<"The time for process number"<<" "<< final_result_count <<" "<<"is"<<" "<< final_result[final_result_count] <<endl<<endl;
    }

    f.close();
    g.close();
}

在我的退出文件中,它只寫為 0。

我不知道為什么會發生這種情況。 我認為從文件中讀取的文本有問題。 在那里,帶有“ a ”向量。 我是 C++ 的菜鳥。

樂:

# include <iostream>
# include <fstream>
# include <string>
# include <sstream>
# include <streambuf>

using namespace std;

int main ()
{

    ifstream f;
    ofstream g;
    f.open("Processes.txt");
    g.open("Results.txt");
    
    long startTime[1000],endTime[1000];

    string nextLine;
    int lineNum = 1;

    while (getline(f, nextLine)) 
    {
        
    getline(f, nextLine);
    
    startTime[lineNum] = stoi(nextLine.substr(14,8));
    g<<startTime[lineNum]<<"xxxx";
    
    endTime[lineNum+1] = stoi(nextLine.substr(27,8));
    g<<endTime[lineNum]<<"xxxx";
    
    getline(f, nextLine);
    getline(f, nextLine);
    getline(f, nextLine);
    
    lineNum=lineNum+3;
    }

    f.close();
    g.close();
}

輸入文件:

------------------------------------------------------------------------------
started at: *00000005  Number: 1
completed at: #00000065  Number: 1
------------------------------------------------------------------------------
------------------------------------------------------------------------------
started at: *00000000  Number: 2
completed at: #00000100  Number: 2
------------------------------------------------------------------------------
------------------------------------------------------------------------------
started at: *09999999  Number: 3
completed at: #10000000  Number: 3

正如我相信您最近的評論所述,您已經找到了處理此問題的最佳方法。 您想讀取每一行並解析字符串。 類似如下:

std::string nextLine;
int lineNum = 0;

while (std::getline(f, nextLine)) {
    startTime[lineNum] = std::stol(nextLine.substr(14,8));
    endTime[lineNum] = std::stol(nextLine.substr(50,8));
    lineNum++;
}

公平警告:我實際上並沒有仔細計算這些偏移量,所以我可能會偏離一些。 此外,您還必須考慮那些只有破折號的“空白”線。 但我會讓你去打掃房間。

希望這能讓你走上正確的道路!

暫無
暫無

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

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