Reputation: 197
#include <stdio.h>
int main()
{
char str[] = "[email protected], 22:44:45";
char user[45], email[45];
int h, m, s;
sscanf(str, "%44s@%44s, %d:%d:%d", user, email, &h, &m, &s);
printf("User: %s | Email: %s | Hour: %d | Minutes: %d | Seconds: %d\n", user, email, h, m, s);
return 0;
}
Output: User: [email protected], | Email: garbage | Hour: garbage | Minutes: garbage | Seconds: garbage
Why is it storing everything up to , into user?
Upvotes: 0
Views: 182
Reputation: 780994
%44s@
doesn't stop reading the string when it gets to the @
character. It means to read a string until whitespace, up to 44 characters. Then it needs to read a @
character after that in order to continue parsing the rest of the string.
To read characters up to @
, use %44[^@]
. Then follow this with @
to skip over that character before extracting the next string.
sscanf(str, "%44[^@]@%44[^,], %d:%d:%d", user, email, &h, &m, &s);
Upvotes: 2