[英]Extracting number from file C++
In my program I want to extract a number from a line (which represents a stop time in seconds) from which I cut down another number that I extract from the next line (which represents a start time in seconds) and after that writing the result in the Results file.在我的程序中,我想从一行中提取一个数字(代表以秒为单位的停止时间),从中我减少我从下一行提取的另一个数字(代表以秒为单位的开始时间),然后写入结果在结果文件中。
I have the following code:我有以下代码:
# 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();
}
In my exit file it is written just 0.在我的退出文件中,它只写为 0。
I don't know why this happens.我不知道为什么会发生这种情况。 I think there is something wrong with the text reading from the file.
我认为从文件中读取的文本有问题。 There, with the " a " vector.
在那里,带有“ a ”向量。 I am noob with C++.
我是 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();
}
INPUT FILE:输入文件:
------------------------------------------------------------------------------
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
As I believe your most recent comment states, you have found the best way to handle this.正如我相信您最近的评论所述,您已经找到了处理此问题的最佳方法。 You want to read each line and parse the string.
您想读取每一行并解析字符串。 Something like follows:
类似如下:
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++;
}
Fair warning: I did not actually count those offsets carefully, so I might be off by a few.公平警告:我实际上并没有仔细计算这些偏移量,所以我可能会偏离一些。 Also, you'll have to account for those "blank" lines of only dashes as well.
此外,您还必须考虑那些只有破折号的“空白”线。 But I'll leave you to the housekeeping.
但我会让你去打扫房间。
Hope this puts you on the right path!希望这能让你走上正确的道路!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.