AlexT
AlexT

Reputation: 609

sscanf - Scan certain part of string;

I have a formatted string and want to extract only certain parts of it. For example:

char * str = 618/3171 259/1429 557/2842;

Through sscanf I want three variables (integers) to hold the following variables:

i = 618
j = 259
k = 557

I can't seem to get the formatting right. Here is what I've tried:

sscanf(str, "%d %d %d", &i, &j, &k);

which returns:

i = 618
j = 0
k = 0

Thanks in advance for the help

Upvotes: 1

Views: 939

Answers (1)

Minos
Minos

Reputation: 218

Try this code.

int main()
{
    const char *str = "618/3171 259/1429 557/2842";

    int i, j, k;
    sscanf(str, "%d/%*d %d/%*d %d/%*d", &i, &j, &k);

    printf("%d %d %d", i, j, k);
    return 0;
}

asterisk (*) skips value. so don't need parameter for that.

Upvotes: 3

Related Questions