I have a string that contains information separated by | characters, and I need to create substrings for each piece of information. Additionally, some of the fields may be blank.
The order of values goes FirstName|LastName|Age|IDNumber|Height|Weight
So possible strings could be:
char* strA = "John|Doe|50|123456|70|150"; char* strB = "James|Smith|40|345678||";
I am able to pull out the first field just fine using sscanf, but when I attempt to grab multiple fields I get the wrong values.
In order to get the first value, I can call
char* strA = "John|Doe|50|123456|70|150";
char* a, b;
sscanf(strA, "%[^|]|", &a);
printf("%s\n", &a);
which prints
John
But when I attempt to get the first 2 fields by calling
char* strA = "John|Doe|50|123456|70|150";
char* a, b;
sscanf(strA, "%[^|]|%[^|]|", &a, &b);
printf("%s | %s\n", &a, &b);
It prints
oe | Doe
I cannot figure out why that is.
&when callingprintf. - Barmaraandbto point to any valid memory, and (b) sendingaandbby address (so the addresses of the pointers, not what they should be pointing to), are compounding your problems. - WhozCraig