fscanf question - invalid memory access error

Discussion in 'C' started by kerrymaid, Aug 31, 2010.

  1. kerrymaid

    kerrymaid New Member

    Joined:
    Aug 31, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I think this is a pretty basic problem but I'm still stumped as to why it's happening:

    I use the following code (making use of a simulator API) to read in from a file and store information in a list (the format of the information is an id number (int) followed by an x and y co-ordinate (both doubles)).

    My code reads everything in fine until I get to an id number that has double digits e.g. 10 at which point it gives me an "invalid memory access" error. If I edit the file to only have entries up until id number 9, it works fine. I cannot understand why this would be the case. It fails on the fscanf line for the file entry with an id number of 10.

    Any help appreciated.
    Code:
            fscanf(intersectionPositions_traj_file, "%d %lf %lf",&int_id,&x_pos,&y_pos);//read the values from file
            
            while(!feof(intersectionPositions_traj_file))
            {                
                    //create a new instance of the struct to hold the data. this will be added to the list
                    int_measurement= (ILS_Intersection_Trajectory_Vals*)op_prg_mem_alloc(sizeof(ILS_Intersection_Trajectory_Vals));
                    int_measurement->int_id=int_id;
                                                                    
                    int_measurement->x_pos=x_pos;
                    int_measurement->y_pos=y_pos;
                    
                    sprintf(stringIP, "%d.%d.%d.%d", int_id, int_id, int_id, int_id);
    
                    intersectionInetTAddress.address.ipv4_addr = *stringIP;
                    int_measurement->int_ip = inet_ipv4_address_get(intersectionInetTAddress);
    
                    op_prg_list_insert(intersection_ptr->intersection_list,int_measurement,OPC_LISTPOS_TAIL);
                    printf("values added to the INTERSECTION list.\n %d\t%lf\t%lf\n",int_measurement->int_id,int_measurement->x_pos,int_measurement->y_pos);
                    
                    fscanf(intersectionPositions_traj_file, "%d %lf %lf",&int_id,&x_pos,&y_pos);//read the values from file
            }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice