The problem is that when scanning for a string, the scanf
family of function will scan until a whitespace (or end of file, whichever happens first). You can't really use pattern-matching when scanning for a string. You can verify this quite simply by checking the return value of fscanf
, in your case it should be 3
(since it scans the integer, a character and then the rest as a single string).
Incidentally this will of course lead to undefined behavior as you overwrite the array allocated for the string by quite a margin, as well as you printing the values of uninitialized local variables.
Instead it might be better to tokenize the input (hint: see the strtok
function), to first split it on the ampersand '&'
, and except the first value then split the other on the equality character '='
. Then check each "key" to know what value to set.