1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

sscanf for a string from a file

Discussion in 'C' started by biaspoint, Sep 7, 2010.

  1. biaspoint

    biaspoint New Member

    Sep 7, 2010
    Likes Received:
    Trophy Points:
    I am reading in a csv that contains 13 columns, the first column is the name of an image, for example "picture.tiff" the next 12 are numerical parameters using a decimal, for example "23.45". I had no problem parsing it when I made everything a number (instead of picture.tiff, I used 0001 for image names and later appended .tiff to it to open)... So I was thinking I would be more efficient and save the complete image name in the csv. However now I can parse the string, but not the numerical parameters following... as usual I think I must be going crazy and need to lay off the coffee. FYI I am saving everything in to a struct.

    char line [ 180 ];
    fp= fopen(csvfilename,"r");
    fgets(line, sizeof line, fp); //bypass first header line
    sscanf(line, "%31s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f", &Initial_Guess[z].ImageName, &Initial_Guess[z].x1, ... (etc....)
    So I get the ImageName correct and can load the image using OpenCV, however all my parameters come back as 0.0000...

    I also tried splitting it up

    char line2[180];
    sscanf(line, "%31s,%s", &Initial_Guess[z].ImageName, line2);
    and then sscanf on line2 but no good,... if I debug and look at "line2" it IS the rest of the line from the csv... here is the definition of the struct if that helps"

    typedef struct
       char ImageName [ 180 ];
       float  x1, y1, z1, roll1, pitch1, yaw1, x2, y2, z2, roll2, pitch2, yaw2;
       CvPoint BoxUL, BoxLR;
    } RECORD_T38;
    And yes, all the images names are exactly the same length, so 31 works good...


    Thanks in advance.

Share This Page