Reading values from text file

This is a discussion on Reading values from text file within the C Programming forums, part of the General Programming Boards category; So Im trying to read some variables from a text file - stored like this: 2.503, 0.500, 1.200, 2.693, 4.325, ...

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

    Reading values from text file

    So Im trying to read some variables from a text file - stored like this:
    2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 5.000, 21.000
    2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 4.938, 21.000
    The second last value has varied, see?
    Here is my code
    Code:
    if ( &file1 != NULL )
       {
    	while(f_gets(buffer, sizeof(buffer), &file1)!= NULL)
        {
         res = sscanf(buffer, "%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf\n", &data[0],&data[1],&data[2],&data[3],&data[4],&data[5],&data[6],&data[7],&data[8]); 
    
            printf_P(PSTR("data: "));
    		printf("%.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n", data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7],data[8]); 
        } 
        f_close(&file1);
    I know its not quite how it would be done on C on a pc - im programming a microcontroller, so the filesystem commands are very slightly different.
    It works to a certain degree - I get this back
    data: 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 4.938, 21.000
    data: 2.503, 0.500, 1.200, 2.693, 4.325, 6.131, 8.125, 4.938, 21.000
    In other words, its reading the last line fine, but doesnt get the first. Any idea what tells the code to grab each line, rather than only the last one?
    Last edited by a.mlw.walker; 01-14-2012 at 11:03 AM. Reason: wanted to hightlight differences

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You should print the value of res too.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Oh yep. res = 0.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, the file pointer in fgets(), is used as the third argument. It's an error to use the address of the file pointer, as you have it apparently with &file1.

    You're sure there is no closing curly brace, on the end of the sscanf() line, right? That's what it seems to work like.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,579
    Quote Originally Posted by a.mlw.walker View Post
    Oh yep. res = 0.
    This result implies it is printing the last line twice.

    The result should be the number of items the scanf read.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Cheers guys. Usually when using microcontroller file system commands, 0 return has meant a sucess. I foolishly assumed 0 here would also mean success before looking into sscanf.
    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading two values from a file as one value?
    By Flotonic in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 05:53 PM
  2. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 06:15 PM
  3. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  4. Replies: 6
    Last Post: 11-11-2006, 01:10 PM
  5. reading values from a line of text
    By Todd in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2002, 04:32 PM

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