: Hi Lundin & homerocda
:
: So.. summing up your advices,
sscanf plays a bit of role like
strtok which to find delimination?
:
:
sscanf only picked up the
first delimination which is Benny. But it doesn't pick up the WHOLE
setence right?
:
: And for the result above, %*s is playing a role.... correct?
:
: But, WITHOUT %*s,
Benny part was still printed out but
29 became
0. I thought the result would be
Benny is 29 years old -> 29, but it wasn't.
:
: OR,
%*s part only affects
%i (the latter formatted input but
%s in
sscanf still only prints out the
first deliminator?
:
tokoG, have you read the explanation for sscanf on that page?
sscanf works just like scanf, but reads from a string not standard input.
%s means "read the next characters until a blank character is found". It would never read the whole sentence unless if it were just a word.
When it founds a match on the string it starts searching for the match for the next pattern "until it finds a blank character", but starting from where the first match stopped.
Without the %*s (which means "ignore the next string") it will try to set the next word to an int. But the next word found is not an integer is "is", which can't be translated to a numerical value, so it just sets the variable to 0.
source string = "Benny is 29 years old"
format string = "%s %*s %i"
1. Trying to match the first pattern as a string in the source string:
source string = "Benny is 29 years old"
+---+
Benny: string found
Expecting: %s -> string
Match! Store it on the first variable passed.
2. Trying to match the second pattern as a string in the source string:
source string = "Benny is 29 years old"
++
is: string found
Expecting: %*s -> string
Match! Uh-oh, there is the * modifier here, don't store the string found.
3. Trying to match the third pattern as an integer in the source string:
source string = "Benny is 29 years old"
++
29: string found
Expecting: %i -> integer
Match! 29 can be converted to an integer, so it matches and stores it on the second variable passed.
There are no more patterns on the format string, stop searching.
It is surprisingly that you have already used strtok but never used scanf.
Homero C. de Almeida
There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.