Jump to content

can someone explain whats sscanf ?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
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
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
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:
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.

Edited by dargueta, 19 March 2010 - 01:39 PM.

sudo rm -rf /

#3
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
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 ?

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
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:
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:

int age;
char *name;
const char *mystring = "Mouhammad age 12";

sscanf(mystring, "%as %*s %d", [U][B]&[/B][/U]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 /

#5
dcs

dcs

    Programming God

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

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Ok...that's weird. I know I edited my first post...
sudo rm -rf /

#7
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
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..

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Oh, you mean you want to know the inner workings of sscanf?
sudo rm -rf /

#9
Moudi

Moudi

    Programmer

  • Members
  • PipPipPipPip
  • 167 posts
Yes please

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
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 /

#11
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Dinkumware, Ltd. - Compleat Libraries Reference