读取数据,epochnum共2880,程序只能读取epochnum = 0到epochnum = 1000所包含的数据,程序生成解决方案时,无错误,但调试时被中断
//OfileRead为结构体
int Oread(OfileRead *epoch,int epochnum)
{
ifstream oifs;
oifs.open("g.txt", ios::_Nocreate);
if (!oifs.is_open())
{
cout << "文件打开失败" << endl;
return -1;
}
cout << "文件打开成功" << endl;
char str[500];
epochnum = 0;
while (1)
{
if (oifs.eof())break;
int k = 0; epoch[epochnum].epochgps = 0;//gps卫星数
oifs.getline(str, 500);
if (strstr(str, "> ") != NULL)
{
sscanf(str, "> %d %d %lf %c ", &(epoch[epochnum].hour), &(epoch[epochnum].min), &(epoch[epochnum].sec), &(epoch[epochnum].flag));
epochnum++;
}
}
oifs.close();
delete[]epoch;
}
调试时,出现错误如图
解答思路:可能是1000以后的程序存入出错,但不清楚是哪里的问题
尝试运行输出,也只能输出前1000的相关数据,后续数据与程序都无法读入输出
可以正确读入所有数据
数组 epoch 小了
delete[]epoch; 删了2次
应该是if (strstr(str, "> ") != NULL) 判断问题,
导致结epoch[epochnum]为空,后面读取epoch里面子元素的值直接报错了
1,VS Debug模式启动程序运行,出错时可以显示出错代码堆栈信息,帮助分析定位
2,
char str[500];
oifs.getline(str, 500);
这2行改成如下试试
char str[4096];
oifs.getline(str, 4096);
3,epochnum = 1000时,文件读上来的数据格式规则吗?
4,Oread函数第一个参数,传递的对象数组大小是多少,超过2880了吗?