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; float 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 に答える