Thread: sscanf weirdness

  1. #1
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    sscanf weirdness

    This makes me go ??wtf??

    Code:
    int pgmvalidity_check(char checkline[256])
    {
       long _key=0;
       short _extended=0, _high_low=0, _intel_motorola=0;
       int _baudrate=0;
       
       puts(checkline);
       getch();
    
       sscanf(checkline,"%d\t%d\t%d\t%d\t%d", &_key, &_extended, &_high_low, &_baudrate, &_intel_motorola);
    
       printf("high_low: %d\n", _high_low);
    
    ....
    
    }
    Output is:
    1 0 1 1 0
    high_low: 0
    Does anyone see what I'm doing wrong? (high_low should be 1 afaik)
    Thanks in advance!

    René
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    try
    Code:
    int _extended=0, _high_low=0, _intel_motorola=0;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    yxunomei is right, you have to be very specific about the types you pass to scanf() functions.

    If you're using gcc (or one of it's many ports like mingw or dev-c++), then use the
    -W -Wall
    command line options to tell you when your printf/scanf control string doesn't match the parameters you pass.
    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.

  4. #4
    Registered User Chirag's Avatar
    Join Date
    Oct 2006
    Location
    India
    Posts
    6

    Lightbulb Hello...

    Hello,

    Firstly you are making a very big mistake of treating a Character array as an Integer. You cannot expect the whole string "1 0 1 1 0" to be the same in integer data-type.

    If you try to find out the Integer substitute of that string, you would have to indivitually convert each characters into ASCII and calculate it. And the answer what you get would get stored in first variable in your sscanf list i.e., _key.

    Now you can do it by two methods. You can use Type Casting or you can directly take Character data-type for extended, _high_low, _intel_motorola.

    Either way it will work...So the point is you need to work on it as a STRING, you cannot directly expect a string to get stored into integer from a stream.

    Hope it helps
    Chirag Chhatbar.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Quote Originally Posted by Chirag
    Firstly you are making a very big mistake of treating a Character array as an Integer.
    Where?

    The original poster's usage of sscanf is correct. The first paramter should be char *
    As already pointed out the problem is with the format specifier for long and short

    %ld and %hd may be appropriate

  6. #6
    Registered User Chirag's Avatar
    Join Date
    Oct 2006
    Location
    India
    Posts
    6

    Smile

    Okay i think you don't understand my point. When sscanf is storing the value into _key...it is storing the ASCII substitute of the whole String.

    Let me just post what i think is a Solution to the problem, yes use of sscanf is CORRECT, but is not what he wanted...

    Code:
    int pgmvalidity_check(char checkline[256])
    {
       char _key=0;
       char _extended=0, _high_low=0, _intel_motorola=0;
       char _baudrate=0;
       
       puts(checkline);
       getch();
    
       sscanf(checkline,"%c\t%c\t%c\t%c\t%c", &_key, &_extended, &_high_low, &_baudrate, &_intel_motorola);
    
       printf("high_low: %c\n", _high_low);
    
    ....
    
    }

  7. #7
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Thanks for your help guys, well appreciated! I indeed had to change the declaration of _high_low from short to integer; it works.

    Thanks again, René
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Chirag
    Okay i think you don't understand my point. When sscanf is storing the value into _key...it is storing the ASCII substitute of the whole String.
    nah, if you want sscanf to interpret the string as integers you can use %d of course. but you have to make sure you pass the correct pointers to the variables that will receive the values.

    he wants to get 1 and 0 in his variables and not '1' and '0'

  9. #9
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Quote Originally Posted by Chirag
    Okay i think you don't understand my point. When sscanf is storing the value into _key...it is storing the ASCII substitute of the whole String.

    Let me just post what i think is a Solution to the problem, yes use of sscanf is CORRECT, but is not what he wanted...

    Code:
    int pgmvalidity_check(char checkline[256])
    {
       char _key=0;
       char _extended=0, _high_low=0, _intel_motorola=0;
       char _baudrate=0;
       
       puts(checkline);
       getch();
    
       sscanf(checkline,"%c\t%c\t%c\t%c\t%c", &_key, &_extended, &_high_low, &_baudrate, &_intel_motorola);
    
       printf("high_low: %c\n", _high_low);
    
    ....
    
    }
    i did this
    Code:
    #include<stdio.h>
    
    int main() {
      char s[10];
    
      fgets(s, 10, stdin);
    
      int a;
      int b;
    
      printf("%s\n", s);
    
      sscanf(s, "%d %d", &a, &b);
    
      printf("%d %d\n", a, b);
    
      return 0;
    }
    and it works just fine when I put some integer value on the fgets...

  10. #10
    Registered User Chirag's Avatar
    Join Date
    Oct 2006
    Location
    India
    Posts
    6
    I try to do the same, but in my case what happens whatever i provide as INPUT (i.e., 10110) it gets stored into only variable a and none of it even gets stored into b. i don't know why...

  11. #11
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    rite... u have to put spaces "1 0 1 1 0"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. sscanf()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 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