0

我对这些东西还是很陌生。我正在尝试为我的高级项目编写一个简单的解析器。我大部分时间都在工作,但是当我让它解析越来越长的文件时,它开始给我一个访问冲突错误。这是我正在解析的示例(.txt 文件中的 APRS 数据包)

> # 1342977832 Sun Jul 22 11:23:51  2012 WB1SAR-11>APT3A2,WIDE1-1,WIDE2-1:/172347h4014.07N/11004.38WO227/015/A=047664/HARBOR

在做了一些研究之后,问题显然出在带有指针的 for 循环中,但我不知道该怎么做。谢谢你的帮助。代码是: int GetLat(char* parse_test, char* line_string) { int B_GPS_lat_deg = 0; 浮动 B_GPS_lat_min = 0;

    parse_test = strstr(line_string,"h"); //the latitude starts after the time.  time ends with "h".  
    char deg_buffer[2]; // buffer to use with atof() which converts an array to a float
    for (int k = 0; k < 2; k++)// skip the "h" and load the first 2 characters after that into buffer
    {
        parse_test++;
        deg_buffer[k] = *parse_test;
    }

    B_GPS_lat_deg = atof(deg_buffer);// convert to float     

    char min_buffer[5];  // buffer for the minutes
    for (int k = 0; k <= 4; k++) // copies the minutes from the parse_test to the buffer
    {
        parse_test++;
        min_buffer[k] = *parse_test;
    }  
    B_GPS_lat_min = atof(min_buffer); //convert to float

    gps_ball_lat = B_GPS_lat_deg+(B_GPS_lat_min/60); //convert from ddmm.mm to decimal degrees dd.dddd
    cout << gps_ball_lat << "\n";

    return(0);

}

4

2 回答 2

0

问题是 strstr() 可以返回 NULL 并且是。我没有检查这个,这导致了这个问题。这是我为我的代码修复它的方法。我添加了“if”语句。

parse_test = strstr(line_string,"h"); //the latitude starts after the time. time ends with "h".
if(parse_test == NULL) // if it can't find an "h" skip this line and continue parsing the file continue; char deg_buffer[3]; // buffer to use with atof() which converts an array to a float

感谢大家的帮助,即使它没有解决这个问题,我相信它已经清理了我的代码并阻止了其他人!!!!

于 2013-11-07T00:10:18.197 回答
0

当您尝试访问未分配给程序的内存时,会发生访问冲突。检查您访问的数组索引是否小于 0 且大于数组长度。

例如这里:

parse_test++; //<---- Are you sure that parse_test has enough chars?
deg_buffer[k] = *parse_test;

您假设 parste_test 具有多于或等于 k ​​个元素。

于 2013-11-06T15:37:44.127 回答