Thread: sscanf problems ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    11

    sscanf problems ...

    Hi im currently reading "teach yourself C in 21 days" from SAMS. I'm at chapter 14 "working with the screen, printer and keybord).

    Well i decided to do some testing with sscanf. I have this file with this contents:

    Code:
    A807091452|11571719|+2.168403E-07|T|P
    A807091454|11571719|+2.170490E-07|T|P
    A807091454|11550661|+2.113044E-07|T|P

    This is the code:

    Code:
    int get_mean(FILE *fp)
    {
         int i = 0;
         
         char  date[MAX+1],
    		  serial[MAX+1],
    		  measure[MAX+1],
    		  buff[MAX+1];
         float mes;
         
         
         while( (fgets(buff, 100, fp)) != NULL) {
             sscanf(buff, "%*c %[1234567890] %*c %[1234567890] %*2c %2.2f ", date, serial, &mes);
             printf("%s %s %f\n", date, serial, mes);
         } 
        return 0;
    }
    In my opinion the fisrt "A" is read bij %*c, the next string "807091454" bij %[1234567890] the "|" by %*c, then "11571719" by %[1234567890] "|+" by %*2c and then the float "2.168403".

    But is does not do what i expect i to do...
    when i run the program this comes out:

    Code:
    807091452 11571719 0.000000
    807091454 11571719 0.000000
    807091454 11550661 0.000000
    Can somebody give me a clue ?
    Last edited by pic-o-matic; 07-30-2008 at 09:41 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't fit 2.2 into a scanf specification (i.e., no fair specifying precision -- you can specify field width). So it's trying to match the literal characters "%2.2f".

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Do you know that you can check the return value of scanf?
    as well as %n format to check at what position the successfull scanf was finished?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    Thx for helping!. I use "%8f" for the float and now it works just fine. Using the return value of scanf is also a good tip, i did know it but did not think about it. As a newbe i have a lot to remember .

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by pic-o-matic View Post
    Thx for helping!. I use "%8f" for the float
    adding width specifier to formats used for parsing string data seems even more important to avoid buffer overruns
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while( (fgets(buff, 100, fp)) != NULL)
    Where did 100 come from?
    Why not sizeof(buff) instead?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf matching problems
    By KBriggs in forum C Programming
    Replies: 10
    Last Post: 06-23-2009, 02:20 PM
  2. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  3. Simple sscanf mystery
    By registering in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 11:47 PM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM