sscanf for a string from a file

This is a discussion on sscanf for a string from a file within the C Programming forums, part of the General Programming Boards category; I am reading in a csv that contains 13 columns, the first column is the name of an image, for ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    4

    sscanf for a string from a file

    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.

    Code:
    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

    Code:
    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"

    Code:
    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...

    Thoughts?

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    sscanf(line, "%31s,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f", &Initial_Guess[z].ImageName, &Initial_Guess[z].x1, ... (etc....)
    & is not needed here. Question 12.12b
    I didn't see any problem here. You should check return value of sscanf(). (read doc)
    Edit, the problem is coz of %31s. sscanf will read 31 characters as string.
    Last edited by Bayint Naung; 09-06-2010 at 07:15 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    You are right, I do not need the & for the ImageName, good catch... however I need the 31 so that only the first 31 items from the csv get put into ImageName, and that portion actually works, I have verified it. I tried it without the 31 and it did not help with the rest of the numerical values...

    btw, z=counter number, should of said that before.

    Thanks

  4. #4
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    This is not C's forte. But you can do something like:
    Code:
    sscanf("%31[^,],%f, ...........
    The %[^,] is similar to %s, in that it reads strings. But this will read all characters up to a comma. It's sort of like a very very basic regular expression. The 31 acts the same as it does for %s.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    You know I read that in one of the manuals, but couldn't get the syntax right... I actually can use:
    Code:
    sscanf(line, "%[^,] %f,%f,...
    and not need the 31... but that still gets me back to where I started, a good parsing of the image name, but not the numbers.

    I wonder if I am debugging this the wrong way...

    Here is the line from the csv file:

    00000_00000000243648463966.tiff,21.85,-0.45,5.5,2,38.5,0.4,0,0,0,0,0,0

    Code:
    printf("%s",line);
    gives me the exact line back

    Code:
    printf("%s", Initial_Guess[z].ImageName);
    gives me: 00000_00000000243648463966.tiff

    and if I parse it using the line2 option...
    Code:
    printf("%s\n",line2);
    I get:
    21.85,-0.45,5.5,2,38.5,0.4,0,0,0,0,0,0

    If I then parse line2 and then try

    Code:
    printf("%f\n",  &Initial_Guess[z].x1);
    I get: 0.000000

    Can't see right off what I could be messing up, but obviously something.
    Last edited by biaspoint; 09-06-2010 at 08:20 PM. Reason: correcting for nomenclature

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    4
    Yep, it sure was the debugging (Doh!)... no need for the "&" in printf!! Thanks for the [^,] I will have to use that again in the future...

    Amazing how much long days, affect the little stuff.

    Thanks for the help!

    Here is the working code for all:
    Code:
    int z =0;
    const char name[]="tiff.csv";	//Look in specified folder for tiff.csv
    char *csvfilename=NULL;
    csvfilename = (CHAR*)malloc(strlen(dir_images)+strlen(name));  // dir_images is a string with the directory of the images
    strcpy(csvfilename,dir_images);
    strcat(csvfilename,name);
    char line [ 280 ];
    FILE *fp;
    	fp= fopen(csvfilename,"r");
    	fgets(line, sizeof line, fp); //bypass first header line
    	while ( z<i)
    	{
    		fgets(line, sizeof line, fp);
    		sscanf(line, "%[^,],%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f", &Initial_Guess[z].ImageName, &Initial_Guess[z].x1,
    	                        &Initial_Guess[z].y1, &Initial_Guess[z].z1, &Initial_Guess[z].roll1, &Initial_Guess[z].pitch1, &Initial_Guess[z].yaw1,
    				&Initial_Guess[z].x2, &Initial_Guess[z].y2, &Initial_Guess[z].z2, &Initial_Guess[z].roll2,
    				&Initial_Guess[z].pitch2, &Initial_Guess[z].yaw2);
                    z++;
    	}
    	z=0;
    	printf("%f %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n", Initial_Guess[z].x1,
    					Initial_Guess[z].y1, Initial_Guess[z].z1, Initial_Guess[z].roll1, Initial_Guess[z].pitch1, Initial_Guess[z].yaw1,
    					Initial_Guess[z].x2, Initial_Guess[z].y2, Initial_Guess[z].z2, Initial_Guess[z].roll2,
    					Initial_Guess[z].pitch2, Initial_Guess[z].yaw2);  // Show me the first one for error checking
    	fclose(fp);
    	free(csvfilename);
    Last edited by biaspoint; 09-06-2010 at 08:37 PM. Reason: Show full Code

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Buggy debugging printf...
    But hey if you are using gcc, gcc -Wall will give you warning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21