Thread: strange behaviour of sscanf

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Question strange behaviour of sscanf

    I got this piece of code:
    PHP Code:
    char *bufpath alias;
    printf("Buf: %s \n"buf);
    if(
    sscanf(buf"%*s %s %s"pathalias)!=2)
    {
        
    ErrorOut(ERR_WRONGPAR);
    }
    printf("Buf: %s \n",buf);
    printf("Path: %s\nAlias: %s\n"pathalias); 
    it is somewhere in a function, which gets buf as parameter.
    Now the output in the console is:

    Buf: PATH U ON
    Buf: ON
    Path: ON
    Alias: ON

    my problem lies between the first and second line, as u can see buf is somehow altered, allthough only sscanf is called.
    Hope i didnt explain it too bad, thanks for any help.

    edit: Forgot to say that the declaration actually isnt directly above the code its just there to show the types of each variable

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If that's an exact copy of your code and not some poor attempt at abbreviation so that we can "find the problem more easily", then buf doesn't point to any memory that you own, so the behavior is undefined and your working with garbage. Otherwise, you've introduced several bugs and I suggest that you post your code exactly.
    Last edited by Prelude; 12-16-2004 at 12:44 PM.
    My best code is written with the delete key.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    scanf() and parsing strings can be tricky business. Typically the issue arrises when when you call something a string and scanf() wants to call it a number. But even at that, its not scanf() doing something wrong as much as you are wanting it to work outside its default behavior. Thats the typical downfall of the scanf() functions. The other common issue with scanf() is when it handles white space in a manner that is unexpected. As for your code, I don't see where the common "my scanf() function is failing" type scenarios:

    Code:
    if(sscanf(buf, "%*s %s %s", path, alias)!=2)
    {
        ErrorOut(ERR_WRONGPAR);
    }
    Do go ahead and explain this part for me.
    Last edited by master5001; 12-16-2004 at 03:09 PM.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    actually this is the function:
    PHP Code:
    int ExecOrder(char *bufint acceptfd)
    {
           
    char *order;
           
    sscanf(buf"%s "order);
           if(
    strcoll(order"PATH")==0)
           {
                   
    char *path, *alias;
                   
    int Path;
                   
    printf("Buf: %s \n"buf);
                   if(
    sscanf(buf"%*s %s %s"pathalias)!=2)
                   {
                           
    ErrorOut(ERR_WRONGPAR); 
                   }
                   
    printf("Buf: %s \n",buf);
                   
    printf("Path: %s\nAlias: %s\n"pathalias);
                   if(
    strcoll(path"U")==0)
                   {
                           
    Path 0x04;
                   }else if(
    strcoll(path"IP")==0)
                   {
                           
    Path 0x10;
                   }else if(
    strcoll(path"I")==0)
                   {
                           
    Path 0x08;
                   }else
                   {
                           
    ErrorOut(ERR_WRONGPAR);
                   }
                   if(
    strcoll(alias"ON")==0)
                   {
                           
    Path Path || 0x80;
                   }else if(
    strcoll(alias"OFF")==0)
                   {
                           
    Path Path && 0x79;
                   }else
                   {
                           
    ErrorOut(ERR_WRONGPAR); 
                   }
     
                  
    SetPath(Path);

                   
    msg="OK";

                   
    len strlen(msg);

                    
    bytes_sent send(acceptfdmsglen0);
                   
    close(acceptfd);
                   return 
    0;
           }else if(
    strcoll(order"RANGE")==0)
           {
                   
    char *range;
                   if(
    sscanf(buf"%s %s"orderrange)!=2)
                   {
                           
    ErrorOut(ERR_WRONGPAR);
                   }

                   if(
    strcoll(range"20A")==0)
                   {
                           
    SetRANGE(0xc1);

                   }else if(
    strcoll(range"10A")==0)
                   {
                           
    SetRANGE(0xc2);

                   }else if(
    strcoll(range"1A")==0)
                   {
                           
    SetRANGE(0xc3);

                   }else if(
    strcoll(range"100mA")==0)
                   {
                           
    SetRANGE(0xc4);

                   }else if(
    strcoll(range"10mA")==0)
                   {
                           
    SetRANGE(0xc5);

                   }else if(
    strcoll(range"1mA")==0)
                   {
                           
    SetRANGE(0xc6);
                   }else
                   {
                           
    ErrorOut(ERR_WRONGPAR);
                   }

                   
    msg="OK";
                   
    len strlen(msg);
                   
    bytes_sent send(acceptfdmsglen0);
                   
    close(acceptfd);
                   return 
    0;
           }
           
    ErrorOut(ERR_UNKNOWNCMD);


    the strange thing is, that the range part is working perfectly, while the path is making that problems.
    I really appreciate that you try to help me, thanks.
    Last edited by BeBu; 12-16-2004 at 03:44 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have to admit, I don't know how Prelude knew you weren't allocating memory but she did. Very impressive oh wise one.

    Code:
    if(sscanf(buf, "%*s %s %s", path, alias)!=2)
    {
        ErrorOut(ERR_WRONGPAR);
    }
    ..is still wrong.

    And like Prelude said, you need to allocate memory.

    Example:
    Code:
    char *order = malloc(BUFFER_SIZE);  // you need to define BUFFER_SIZE somewhere
    
    if(order == NULL) {
      // error handling
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >..is still wrong.
    How so? Both path and alias are pointers to char (albeit, uninitialized pointers, but your correction suggests the format modifier is the problem). What the sscanf call does is read and discard one word (notice the asterisk modifier), then read another word and copy it to path, and read another word and copy it to alias. On a successful call, 2 will be returned because only 2 items were copied. The only real problem at this point is path and alias pointing into limbo, and of course, the lack of field widths producing a gets-like overflow risk.
    Last edited by Prelude; 12-16-2004 at 04:12 PM.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    20

    Smile

    thanks guys i love

    allocating space helped

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > How so?

    Oh! Holy crap! I didn't even notice he did a "%*s" on the first one. See this is where reading stuff before posting is handy

    And yeah I did notice he wasn't initializing neither path nor alias.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behaviour using OpenGL types
    By Hubas in forum C++ Programming
    Replies: 4
    Last Post: 10-31-2003, 04:02 PM
  2. Strange Direct Input Behaviour
    By Diamonds in forum Windows Programming
    Replies: 2
    Last Post: 06-23-2003, 04:34 PM
  3. Strange behaviour of ODBC cursor
    By knutso in forum Windows Programming
    Replies: 6
    Last Post: 05-07-2003, 07:39 AM
  4. GetClientRect strange behaviour
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2002, 02:13 PM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM