ricknaght
ricknaght

Reputation: 93

Input in C, special case?

Good day,

I've got the following code:

 43     while (TRUE)
 44     {
 45         printf("Swipe Card: ");
 46         scanf("%s;%s=%s", id, banner, cp);
 47         printf("%s\n%s\n%s\n", id, banner, cp);
 48         ProcessStudent(banner, file);
 49
 50     }

I've dynamically allocated id, banner, and cp, however when I try to print them (which I did just to check their contents) everything is taken into 'id' only. The string I'm trying to read looks like this %GRE068?;01540594=000331!

Upvotes: 0

Views: 113

Answers (1)

Barmar
Barmar

Reputation: 781706

scanf doesn't try to do a full pattern match of the format string. %s input format simply reads everything up to the next whitespace (or EOF). After that, it looks for a ;, and since it doesn't find that it doesn't parse any of the other inputs.

If you want to stop at some other character, use [^char]

scanf("[^;];%[^=]=%s", id, banner, cp);

Upvotes: 3

Related Questions