--- In [email protected], "Brett McCoy" <[EMAIL PROTECTED]> wrote:
>
> On Dec 12, 2007 8:54 PM, mano M <[EMAIL PROTECTED]> wrote:
> 
> > i need to use %s and %d in sscanf function. It is not
> > actually working as I expected .I simulated the issue
> > using a sample program.
> >
> >   #include<stdio.h>
> > int main()
> > {
> > char str[100]="hello|67";
> > char s1[10];
> > int num;
> > sscanf(str,"%s|%d",s1,&num);
> > printf("%s",s1);
> > }
> >
> >   output: hello|67
> 
> I don't think sscanf will parse string like that without
> whitespace, it's going to read the entire string at once.
> I think strtok() will work better for splitting the string
> on the | character.

Furthermore sscanf() does not provide any error checking such as
buffer overflow or the like. From this point of view I recommend using
 fgets() to retrieve the string and then (as mentioned by Brett)
either strtok() or some other function to split the string into its
logical pieces. It might look a bit clumsy at first glance, but it's
far safer to use than sscanf() and the like.

My 2 cents.

Regards,
Nico

Reply via email to