sscanf from text file

This is a discussion on sscanf from text file within the C Programming forums, part of the General Programming Boards category; Hi I am using this code to read from a text file: Code: result = strtok( buffer, delims ); i=0; ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    192

    sscanf from text file

    Hi I am using this code to read from a text file:
    Code:
    result = strtok( buffer, delims );
    			i=0; count = 0;
    				while (result != NULL)
                {
    			count++;
                sscanf (result, "%f,", &val); //Scan that token into your placeholder
                data[i] = val;
                printf("%7.3f ,", data[i]); //Print out the array
     			i++;
                result = strtok (NULL, delims);
    
                }
    			
    			}
    It works perfectly except for one small detail
    the text file holds this line of data:
    1.000000, 2.503000, 0.500000, 1.200000, 2.693000, 4.325000, 6.131000, 8.125000, 31.415928, 21.000000
    the above code ouputs this

    1.000 , 2.503 , 0.500 , 1.200 , 2.693 , 4.325 , 6.100 ,31000.000 , 8.125 , 31.416 , 21.000 ,

    close except where it says 6.100, 31000.000
    why's it done that? there's no comma there but yet it split it up in a weird way?

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you might have some hidden character in your file. look at it with a hex editor or retype the data in a new file

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    What is buffer, and how did you get the string into it?

    What is data, and how big is it?

    What type is var - is it a float, if it isn't, your sscanf is wrong.

    Try it in isolation - this works!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    int main ( ) {
        char buff[] = "1.000000, 2.503000, 0.500000, 1.200000, 2.693000, 4.325000, 6.131000, 8.125000, 31.415928, 21.000000";
        double data[20];
        char *p = strtok(buff,",");
        int i = 0;
        while ( p != NULL && i < 20 ) {
            sscanf(p,"%lf",&data[i]);
            printf("%f\n",data[i]);
            i++;
            p = strtok(NULL,",");
        }
        return 0;
    }
    
    
    1.000000
    2.503000
    0.500000
    1.200000
    2.693000
    4.325000
    6.131000
    8.125000
    31.415928
    21.000000
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    For some reason i only get this output:
    1.000000, 2.503000, 0.500000, 1.200000, 2.693000, 4.325000, 6.131000, 8.125000, 31.415928, 21.000000
    data[0] = 1.000000
    data[1] = 2.503000
    data[2] = 0.500000
    data[3] = 1.200000
    data[4] = 2.693000
    data[5] = 4.325000
    data[6] = 6.131000
    data[7] = 8.125000
    data[1] = 2.503000
    Code:
     f_gets(buffer, sizeof(buffer), &file1);
    		 	
    			i=0;
    			result = strtok( buffer, delims );
     				while ( result != NULL && i < 10 ) {
    					sscanf(result,"%lf",&data[i]);
            			printf("data[%d] = %f\n",i, data[i]);
            				i++;
            			result = strtok(NULL,",");
        				}
    I dont get the last two?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Ahh, by commenting out this line:
    Code:
    result = strtok(NULL,",");
    it prints upto data[9], however they are all 1's.
    bring it back in ,and it cant print past data[7]?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How big is your buffer?

    I'm thinking your data is being clipped.

    It has to be large enough to hold at least one line... I'd suggest large enough to hold the longest possible line in the file.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    192
    Ok yeah, my buffer was being clipped.
    I have this now:
    Code:
    f_gets(buffer, sizeof(buffer), &file1);
    		 	
    			i=0;
    			result = strtok( buffer, delims );
     				while ( result != NULL/* && i < 10*/ ) {
    					sscanf(result,"%lf",&data[i]);
            			printf("data[%d] = %f\n",i, data[i]);
            				i++;
            			result = strtok(NULL,",");
    					printf("result = %d\n", result);
        				}
    f_gets is the same as the normal fgets function - but I am using a microcontroller - its slightly different syntax.

    That works fine however it only reads the last line of the text file. The file holds this:
    1.000, 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 31.416, 21.000
    2.000, 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 31.102, 21.000
    3.000, 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 30.159, 21.000
    4.000, 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 30.473, 21.000
    5.000, 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 30.788, 21.000
    and it only prints the line with 5.000 on?
    I want to run a function each time data has been filled. I.e after data[9] is full run a function.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have numbers, not text, (except for the commas).

    I would take int the data as numbers, and never use strtok on them!

    Make a loop, and put your data into an array of double (float if you must), and when the array has 9 rows of data, (how many numbers per row is up to you), then call your function.

    Ez, no?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm with Adak on this one. I'd be prone to read the file using fscanf()... It's going to be a whole lot simpler than trying to read a string then have to convert it yet again...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading a file in C, with fgets()/sscanf()
    By ErickN in forum C Programming
    Replies: 3
    Last Post: 04-23-2011, 11:54 AM
  2. sscanf for a string from a file
    By biaspoint in forum C Programming
    Replies: 6
    Last Post: 09-06-2010, 09:35 PM
  3. sscanf does'nt parse a simple file correctly
    By seidel in forum C Programming
    Replies: 2
    Last Post: 02-12-2008, 07:15 AM
  4. Using sscanf to read in the file, Help please!
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 09-05-2002, 06:55 AM
  5. Replies: 4
    Last Post: 06-22-2002, 01:23 PM

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