从文件中提取数字

Extracting number from file C++

本文关键字:数字 提取 文件      更新时间:2023-10-16

在我的程序中,我想从一行(代表以秒为单位的停止时间)中提取一个数字,从中我从下一行(代表以秒为单位的开始时间)中提取另一个数字,然后在结果文件中写入结果。

我有以下代码:

# 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。

我不知道为什么会这样。我认为从文件中读取的文本有问题。那里,有"一个";向量。我是c++的新手。

LE:

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

公平警告:我实际上没有仔细计算这些偏移量,所以我可能会有一些偏差。此外,您还必须考虑那些只有破折号的"空白"行。但我把你交给客房服务。

希望这能让你走上正确的道路!