努力不咕咕 2022-08-18 11:36 采纳率: 88.2%
浏览 29
已结题

C++程序读入文件一直正在运行,读入数据无法选特定元素读入

问题遇到的现象和发生背景

编写导航星历文件读入的时候,生成解决方案没有错误,但调试和运行都无法结束

问题相关代码,请勿粘贴截图

int Nread(int &num)
{
ifstream ifs;
ifs.open("brdm1120.21p", ios::_Nocreate);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return -1;
}
cout << "文件打开成功" << endl;
const int max = 100;//每行字符数
char line[max];
//计算星历数
int line1 = 0;
while (1)
{
if (strstr(line, "END OF HEADER") != NULL)
break;
}
while (1) {
if (strstr(line, "RO1") != NULL)
break;
GPSTIME *pGpsTime = new GPSTIME[num];
NfileRead *ephlst = new NfileRead[num];

    int PRN;
    char column_1[100];
    int year, mon, day, hour, min;
    double sec;
    double a0, a1, a2;
    double iode, Crs, delta_n, M0;
    double Cuc, e, Cus, sqrt_a;
    double toe, Cic, OMG0, Cis;
    double i0, Crc, omgs, OMG;
    double idot, code, GPS_Week, L2_P;
    double SVaccuracy, SVhealth, TGD, IODC;
    double messeagetime, fitinterval;

    char str[max];
    ifs.getline(str, max, '\n');//读取第一行数据
    sscanf(str, "%s %d %d %d %d %d %d %lf %lf %lf", column_1, &year, &mon, &day, &hour, &min, &sec, &a0, &a1, &a2);
    int prn = 0;
    sscanf(column_1, "G%d", &prn);
    cout << hour << "    " << min << "    " << sec << "   " << prn << endl;
    ifs.getline(str, max, '\n');//读取第二行数据
    sscanf(str, "%lf %lf %lf %lf", &iode, &Crs, &delta_n, &M0);
    ifs.getline(str, max, '\n');//读取第三行数据
    sscanf(str, "%lf %lf %lf %lf", &Cuc, &e, &Cus, &sqrt_a);
    ifs.getline(str, max, '\n');//读取第四行数据
    sscanf(str, "%lf %lf %lf %lf", &toe, &Cic, &OMG0, &Cis);
    ifs.getline(str, max, '\n');//读取第五行数据
    sscanf(str, "%lf %lf %lf %lf", &i0, &Crc, &omgs, &OMG);
    ifs.getline(str, max, '\n');//读取第六行数据
    sscanf(str, "%lf %lf %lf %lf", &idot, &code, &GPS_Week, &L2_P);
    ifs.getline(str, max, '\n');//读取第七行数据
    sscanf(str, "%lf %lf %lf %lf", &SVaccuracy, &SVhealth, &TGD, &IODC);
    ifs.getline(str, max, '\n');//读取第八行数据
    sscanf(str, "%lf %lf", &messeagetime, &fitinterval);

    //数据放入数组
    ephlst[line1].year = year;
    ephlst[line1].mon = mon;
    ephlst[line1].day = day;
    ephlst[line1].hour = hour;
    ephlst[line1].min = min;
    ephlst[line1].sec = sec;

    ephlst[line1].PRN = prn;
    ephlst[line1].a0 = a0;
    ephlst[line1].a1 = a1;
    ephlst[line1].a2 = a2;

    ephlst[line1].iode = iode;
    ephlst[line1].Crs = Crs;
    ephlst[line1].delta_n = delta_n;
    ephlst[line1].M0 = M0;
    //&Cuc, &e, &Cus, &sqrt_a
    ephlst[line1].Cuc = Cuc;
    ephlst[line1].e = e;
    ephlst[line1].Cus = Cus;
    ephlst[line1].sqrt_a = sqrt_a;
    //&toe, &Cic, &OMG0, &Cis
    ephlst[line1].toe = toe;
    ephlst[line1].Cic = Cic;
    ephlst[line1].OMG0 = OMG0;
    ephlst[line1].Cis = Cis;
    //&i0, &Crc, &omgs, &OMG
    ephlst[line1].i0 = i0;
    ephlst[line1].Crc = Crc;
    ephlst[line1].omgs = omgs;
    ephlst[line1].OMG = OMG;
    ephlst[line1].idot = idot;
    line1++;
    
}
ifs.close();

}

我的解答思路和尝试过的方法

以为是数据过长,电脑配置导致的错误,只保留GPS数据,运行依旧无法结束

我想要达到的结果

头文件不读入,只从END OF HEADER开始,文件只读入GPS数据和北斗数据并且放入指定数组
文件读入开始部分:

img


GPS数据结尾:

img


北斗数据开头:

img

  • 写回答

2条回答 默认 最新

  • 浪客 2022-08-18 11:48
    关注

    char line[max];
    while (1)
    {
    if (strstr(line, "END OF HEADER") != NULL)
    break;
    }
    这个循环里没有读取操作,循环没意义。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月27日
  • 已采纳回答 8月19日
  • 创建了问题 8月18日

悬赏问题

  • ¥15 请提供一个符合要求的网页链接。
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码