Hi, I am expected to use sscanf function in a program, but I couldn't understand how it works. Can you explain it with an easy example?
Thanks
--
newbiecoder

I just can't understand what happens in the example there :

#include <stdio.h>

int main ()
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;

sscanf (sentence,"%s %*s %d",str,&i);
printf ("%s -> %d\n",str,i);

return 0;
}

what do "i" variable and str[20] array do in this function?
Thanks!

what do "i" variable and str[20] array do in this function?

Edit:: > The i and str[20] variables are declared to store the result of the sscanf function

> As already mentioned (or not) sscanf reads from a buffer (the str[20] string/array)

> sscanf is used to read "things" from a string and put them in variables ...

> I assume you aren't understanding the following line: sscanf (sentence,"%s %*s %d",str,&i); >> sentence is the string where the sscanf function reads from, after the first comma you see the following: "%s %*s %d" where %s tells the sscanf function that the following part it will read will be a string, %*s means that the function will ignore the read value and %d tells the function the next read "thing" should be interpreted as an integer ...

>>After "%s %*s %d" you encounter the following: ,str,&i which tells the function where to put the read values (the first read value will be put in a string called str, the second read value will be ignored (note the asterisk *), and the third read value will be stored in an integer variable ...

>>> Hope this helps :) !

can you tell me why I would ignore the read value with this " %*s"? I understood other parts, thank you very much...

can you tell me why I would ignore the read value with this " %*s"? I understood other parts, thank you very much...

That's just the function's syntax: the '*'-sign indicates that the function has to ignore the following value (not you :P) ...

Edit:: You can find more information on this in the table on this page ...

sentence is the string where the sscanf function reads from, after the first comma you see the following: "%s %*s %d" where %s tells the sscanf function that the following part it will read will be a string, %*s means that the function will ignore the read value and %d tells the function the next read "thing" should be interpreted as an integer ...

In this bold part you say the next read value will be interpreted as an integer but where is the next read value, isnt there only one read value in this code (sentence) ?

Thanks a lot!

In this bold part you say the next read value will be interpreted as an integer but where is the next read value, isnt there only one read value in this code (sentence) ?

Thanks a lot!

sentence is the string where the sscanf function reads from, in the string "Rudolph is 12 years old" you've 4 words and 1 number, but for the sscanf function these are just 5 values:

1) Rudolph
2) is
3) 12
4) years
5) old

Edit:: "%s %*s %d" says that the first value is a string (%s), the second value is also a string (but sscanf has to ignore this one) and the third value is a number (an integer (%d)))

Edit:: sscanf (sentence,"%s %*s %d", str , &i ); the higlighted parts tell the sscanf function where to store the read and non-ignored parts of the string sentence ...

Yeah I really understand now :) thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.