Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: can someone explain whats sscanf ?

  1. #1
    Moudi's Avatar
    Moudi is offline Programmer
    Join Date
    Jan 2010
    Posts
    166
    Blog Entries
    1
    Rep Power
    9

    can someone explain whats sscanf ?

    So i was snooping around and trying to find out what sscanf does, so okay its a better scanf

    int a = sscanf(buffer, "%d", &n);

    if (a != 1)
    {
    // user didn't input an integer in n ;
    }

    Now can someone please explain what the buffer is for ? And i've seen some sscanf's that are transfering stuff from buffer to other variables like sscanf(buffer, "%s %*s %d", &bla, &bla1, &bla2);

    whats %*s.

    Oh well thats all my questions ^_^

    Thanks in advance for anyone that tries to help me

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: can someone explain whats sscanf ?

    Do you know what scanf is for? Because sscanf is exactly the same thing except it reads from a string, not STDIN. That's what the buffer is - the string it's reading from.

    As far as %*s, that's a way of telling the scanf functions that the number of characters you want to read is specified in a variable. For example:
    Code:
    sscanf("%d %32s", &myint, mystring);
    This tells sscanf that you want to read in an integer and at most 32 characters into mystring.

    EDIT: I screwed up and got two format specifiers mixed up, so I removed this bit.
    Last edited by dargueta; 03-19-2010 at 02:39 PM.
    sudo rm -rf /

  4. #3
    Moudi's Avatar
    Moudi is offline Programmer
    Join Date
    Jan 2010
    Posts
    166
    Blog Entries
    1
    Rep Power
    9

    Re: can someone explain whats sscanf ?

    So lets say
    char mystring[] = "Mouhammed age 12";
    char string[10];
    sscanf(mystring, "%9s %d", string, &age);

    That will input Mouhammed in the string " string ", and 12 in the age integer ?

  5. #4
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: can someone explain whats sscanf ?

    No, unfortunately. (I got that confused with the %n specifier. Next time I'll double-check what I post.) In normal convention, the asterisk means "discard this input." Your format string should be like this:
    Code:
    sscanf(mystring, "%9s %*s %d",string,&age);
    If you want to specify a variable size, you can use the 'a' modifier (note this is a GNU/C99 extension) to dynamically allocate a buffer. Important: you have to pass in a pointer to your char * pointer, not just your char * pointer. Take this as an example:

    Code:
    int age;
    char *name;
    const char *mystring = "Mouhammad age 12";
    
    sscanf(mystring, "%as %*s %d", &name, &age);
    
    // do something here with name
    
    free(name);
    Make sure you deallocate name or else you're going to leak memory and probably run out.
    sudo rm -rf /

  6. #5
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: can someone explain whats sscanf ?

    I generally don't advise using non-standard extensions: it will bite you in the ass some day.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: can someone explain whats sscanf ?

    Ok...that's weird. I know I edited my first post...
    sudo rm -rf /

  8. #7
    Moudi's Avatar
    Moudi is offline Programmer
    Join Date
    Jan 2010
    Posts
    166
    Blog Entries
    1
    Rep Power
    9

    Re: can someone explain whats sscanf ?

    Well i'm sounding like pain in the ass i know but let me get this straight, i want to know what it accualy works, now i know how's its syntax..

  9. #8
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: can someone explain whats sscanf ?

    Oh, you mean you want to know the inner workings of sscanf?
    sudo rm -rf /

  10. #9
    Moudi's Avatar
    Moudi is offline Programmer
    Join Date
    Jan 2010
    Posts
    166
    Blog Entries
    1
    Rep Power
    9

    Re: can someone explain whats sscanf ?

    Yes please

  11. #10
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: can someone explain whats sscanf ?

    That's implementation-specific; everyone can pretty much do it their own way as long as they conform to the standards. You should be able to find the GNU implementation online somewhere with Google.
    sudo rm -rf /

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. can you explain this code please ?
    By depro in forum HTML Programming
    Replies: 1
    Last Post: 11-20-2010, 04:16 AM
  2. Can someone explain this to me...?
    By Universal11 in forum C# Programming
    Replies: 7
    Last Post: 11-01-2010, 04:54 AM
  3. can someone explain me hex ?
    By The one who hunts elves in forum The Lounge
    Replies: 2
    Last Post: 05-16-2010, 11:48 PM
  4. Can anyone explain segmentation?
    By facade626 in forum Java Help
    Replies: 1
    Last Post: 01-18-2010, 06:07 PM
  5. quick sscanf question in c
    By DaCapo in forum C and C++
    Replies: 3
    Last Post: 11-30-2007, 09:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts