Question
Scan the string from white space until end using format specifier
I have an existing code that scans a string and stores values in multiple temporary variables.
Here is the sample code:
char str[] = "# # Time=80 interval";
char hash1, hash2;
char timeStr[20];
sscanf(str, "%c %c %[^\0]s", &hash1, &hash2, timeStr);
printf("hash 1: %c\n", hash1);
printf("hash 2: %c\n", hash2);
printf("Time String: %s\n", timeStr);
Actual output is:
hash 1: #
hash 2: #
Time String:
The expected output is:
hash 1: #
hash 2: #
Time String: Time=80 interval
I modified the code and it is working:
sscanf(str, "%c %c **%[^\n]s**", &hash1, &hash2, timeStr);
This is legacy code %[^\0]s
and it is not working now.
Can someone help to understand by any chance why it was working earlier and stopped working now?