从文件中提取数字
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。
我不知道为什么会这样。我认为从文件中读取的文本有问题。那里,有"一个";向量。我是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++;
}
公平警告:我实际上没有仔细计算这些偏移量,所以我可能会有一些偏差。此外,您还必须考虑那些只有破折号的"空白"行。但我把你交给客房服务。
希望这能让你走上正确的道路!
相关文章:
- 如何从也包含C++字母的文本文件中提取某些数字?
- 如何从适合一定范围的数字中提取数字?
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 从迭代器中提取数字
- 如何安全地将有符号字段从uint32_t提取到有符号数字(int或uint32_t)中
- 如何从txt文件c++中提取括号之间的数字
- 如何将ASCII数字转换为从文件中提取的整数
- 如何提取给定数字的 p MSB 位
- printf如何从浮点数字中提取数字
- 使用boost::filesystem和boost::lexical_cast从文件名中提取数字
- 在循环中使用字符串流从几个字符串中提取数字
- 从字母数字 QString 中提取编号
- 从C++字符串中提取数字
- C 正则是从字符串中提取所有可能的10位数字
- 从数组中提取一对数字
- 尝试编写函数尝试从字符串中提取数字,但它返回不相关的数字
- 接受多个输入(如 +、- 和平方数字)的计算器.从文本文件中提取信息
- 长方程加法/减法计算器,从文本文件中提取运算符和数字
- 仅使用关键字和数字提取来解析简单语法
- 将文件中的值存储到数组中,并根据特定数字提取值