Thread: sscanf and parsing invalid floats?

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    sscanf and parsing invalid floats?

    I'm using sscanf to scroll through a file. The file has 4 values divided by spaces:

    92832 John Doe 23.33

    the problem i'm facing is if the file has an invalid float

    92832 John Doe 23/33 and i try assigning it to a variable that value is rounded down to 23.00 how do i detect if it's an invalid float? I know that sscanf returns a number of items it has successfully read i.e. with the above example it would return 4. But even with the invalid float it still returns 4 so i'm kinda stuck??

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    what have you tried doing? we might be able to see your problem from the code?

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    my code is a bit long... ill post down the relevant bits in a sec.

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok this should describe my problem:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct salesRecord
    {
       float budget;
    };
    
    typedef struct salesRecord saleNode;
    
    int main ()
    {
      char sentence []="23/33";
      
      saleNode *test = (saleNode *) malloc(sizeof(saleNode));
    
      sscanf (sentence,"%f", &test->budget);
      
      printf("%f", test->budget);
      
      return 0;
    }
    ./test.c

    23.000000

    rounds it to 23 ?? :|

  5. #5
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    you want to make 23/33 to 23 or 23/33 to 23.33?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > saleNode *test = (saleNode *) malloc(sizeof(saleNode));
    Don't cast malloc in C - see the FAQ

    > char sentence []="23/33";
    > sscanf (sentence,"%f", &test->budget);
    But 23 is a valid float as per sscanfs rules. It stops conversion at the first character which fails to match a floating point number.

    If you want to see what that character is, then perhaps
    Code:
    int n;
    sscanf (sentence,"%f%n", &test->budget,&n);
    if ( sentence[n] ... )
    The %n conversion tells you how many characters have been used so far, and hence which would be the next to be converted. If that character is "invalid" by whatever rules you choose, then you know what to do about it.
    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.

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ok the %n conversion returns "20" basically i just an if that says if it is less than or equal i exit out. Is there anyway to do this without the use of such a number? as it just makes the code more harder to read with random numbers and no meanings (yes i can comment it but it should be naturally readable anyway)

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    nevermind. I used fscan instead and checked the return value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing using strtok() and sscanf()
    By NuNn in forum C Programming
    Replies: 13
    Last Post: 02-12-2009, 02:43 PM
  2. Some questions on sscanf and parsing data
    By green2black in forum C Programming
    Replies: 7
    Last Post: 12-02-2008, 08:25 PM
  3. Replies: 12
    Last Post: 10-16-2008, 12:07 PM
  4. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  5. Parsing a line using sscanf()
    By caduardo21 in forum C Programming
    Replies: 6
    Last Post: 02-10-2005, 07:38 AM