加载大型文本文件(50,000多行,〜8MB)会导致我的程序停止
Loading in large text file (50,000+ lines, ~8MB) causes my program to stop?
回答:
更改
while (tracefile->good()){
getline(*tracefile,line);
....
to
while(getline(*tracefile, line)){
....
做到了。谢谢,@pmr。
原始:
我为我的计算机体系结构类制作了一个缓存模拟器,该类应该在包含我缓存的内存指令的跟踪文件中读取。
我在制作程序时只使用了一个只有1000行的测试跟踪文件,但是实际的跟踪文件为50k 行。通过测试跟踪,程序运行完美。使用实际的跟踪,该程序一直持续到尝试在一条线上使用.substr(),这会导致out_of_range异常并过早地停止我的程序。我调查了,发现getline()在痕迹太大时会提供空字符串。请记住,这不会发生在轨迹< = 〜5000行。
有人知道为什么会发生这种情况吗?我正在使用一个ifstream,如果很重要。
编辑:对不起,这是代码。它不会超越" ...."
主:
cout << "Program started.n";
string str[6];
int i = 0;
/* check for the correct number of arguments */
if(argc != 3){
cout<<"usage: "<< argv[0] <<" <configfile> <tracefile>n";
exit(0);
}
string confname = argv[1];
string tracename = argv[2];
/* open the files and check if it worked */
ifstream confile;
ifstream tracefile;
confile.open (argv[1], ios::in);
if (!confile.is_open()){
cout<<"Could not open config file.n";
exit(0);
}
if (confile.is_open()) cout << "Config file loaded.n";
tracefile.open (argv[2], ios::in);
if (!tracefile.is_open()){
cout<<"Could not open trace file.n";
exit(0);
}
if (tracefile.is_open()) cout << "Trace file loaded.n";
/* read in the data from the config file */
if (confile.is_open()){
i = 0;
while(confile.good() && i != 6){
getline(confile, str[i]);
i++;
}
}
/* create the cache simulator */
cache csim(atoi(str[0].c_str()), atoi(str[1].c_str()),
atoi(str[2].c_str()), atoi(str[3].c_str()),
atoi(str[4].c_str()), atoi(str[5].c_str()));
cout << "Simulator started.n";
/* send the simulator the trace */
csim.trace(&tracefile);
csim.trace:
cout << "Going through trace...";
int i;
string line;
string tag, index, offset;
this->IC = 0;
/* loop until there is no more lines */
while (tracefile->good()){
/* get the next line and increment the instruction counter */
getline(*tracefile,line);
this->IC++;
/* convert hex address to a binary address */
string address = "";
for (i = 0; i < line.substr(4,8).length (); i++)
{
....
看起来您的一个字符串可能还不够长。
while (getline(*tracefile,line);){
/* get the next line and increment the instruction counter */
this->IC++;
/* convert hex address to a binary address */
//be sure to check length of string
if (line.size() < 12)
{
cerr << "line is too small" << endl;
continue;
}
string address = "";
string tmp_str = line.substr(4,8);
for (i = 0; i < tmp_str.size(); i++) //tmp_str.size() will always be 8
{
}
}
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较