0
votes

I am still fairly unfamiliar with this stuff. I am trying to write a simple parser for my senior project. I have it working most of the time, but as I get it to parse longer and longer files it starts giving me an access violation error. Here is an example of what I am parsing (APRS packet in a .txt file)

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

After doing some research it seems obvious that the problem is in the for loops with the pointers, but I don't know what exactly to do about it. Thanks for your help. The code is: 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);

}

2
Are you doing this line by line? The first line doesn't have an "h". Would that break your code? - acfrancis
The chars immediately after "h" are "4014.07". That's 2 chars ("40") for the first loop and you should use 5 chars ("14.07") for the second loop but you use 4 chars instead. Or am I misreading it? - acfrancis
That is right, I have changed it and still have the same problem. - user2961077
I think you need a null character at the end of each string you pass to atof(). Try declaring the buffers with 3 and 6 chars respectively and add a '\0'' to the last char before calling atof() - acfrancis
I think I just tried this, but to verify I am indexing my arrays properly here is an example. deg_buffer is a 3 char array. to make the last value of that array NULL I would use this: deg_buffer[2] = '\0'; - user2961077

2 Answers

0
votes

Acces violations occurs when you try to acces memory that is not given to your program. Check that you are not accesing array indexes lower than 0 and bigger than array length.

For example here:

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

you asume that parste_test has more or equal than k elements.

0
votes

The problem was that strstr() can return a NULL and was. I didn't check for this and this caused the issue. Here is how I fixed it for my code. I added the "if" statement.

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

Thanks to all for your help, even if it didn't fix that problem I am sure that it cleaned my code up and prevented others!!!!