【问题标题】:Extracting number from file C++从文件 C++ 中提取数字
【发布时间】:2015-08-14 08:06: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。

我不知道为什么会这样。我认为从文件中读取的文本有问题。 在那里,带有“ 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

【问题讨论】:

  • 变量名允许超过一个字符。例如,如果您将d[1000] 更改为final_results[1000],程序会更容易阅读。
  • 好的,我会的,谢谢!
  • 伙计,请按照 C++ 编码指南编写代码。这是一个好习惯。
  • @Alexander 没有“C++ 编码指南”,但肯定有常识。
  • 当您的输入以------ 开头时,您希望f &gt;&gt; n 做什么?

标签: c++ file extract


【解决方案1】:

我相信您最近的评论指出,您已经找到了处理此问题的最佳方法。您想读取每一行并解析字符串。如下所示:

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

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

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

【讨论】:

  • 你的代码中有一些奇怪的函数,但我想我理解了。您还跳过了数字结构的部分,您只需按原样“接受”它。问题是,如果线条的长度有时会发生变化,我该怎么办?我想它不适用于那些特殊的数字。无论如何,我想我明白了你的意思,我会挖掘一些 substr 和 stol 的东西。谢谢!
  • 是的,解析字符串有不同的方法。 stringstreams 也适用于解析,如果您的 string 处于通用模式而不是通用索引偏移量。另外,std::stol 是 C++11 的一个特性,所以如果你没有这个特性,你必须自己用 stringstream 重新创建它——只有 3 行左右的函数。
  • 我有一个 dev 4.9.9,当我编译它时说 stol 和 readline 不是来自 std.Ive 谷歌它看起来你是对的,他们是。为什么是那和什么我应该怎么做?我的编译有问题吗? LE:我刚刚看到你写的东西。有什么方法可以在编译器库中插入这些功能吗?不是有一些文件可以让我放置所有缺少的功能吗?
  • 我更新到 Dev-C++ 5.11 并且仍然缺少功能。 :(你能指出我如何用stringstream重新创建它吗?
  • 在一个函数中:std::stringstream ss(nextLine); long temp; ss &gt;&gt; temp; return temp;
猜你喜欢
  • 1970-01-01
  • 2020-12-28
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多