This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sscanf does not work properly

I started to use the sscanf function to parse commands from serial port and found out that it does not work as it must do. Example:

int i, j = 0x1234;
char c = '\0';
i = sscanf("anything", "%d%c", &j, &c);
After the function call:
i == 1,
j == 0,
c == 'a',
while there should have been:
i == 0,
j == 0x1234,
c == '\0'.

Obviously, I cannot detect some errors in the input with this 'non-ANSI' sscanf. Does anyone have any ideas how to solve the problem?

Parents
  • Since sscanf() does not appear to be returning after the first conversion (matching) failure, you might want to consider using the strtol() or strtoul() functions to convert the integer portions of the strings. These functions provide extra "feedback" about the progress of their conversions.

Reply
  • Since sscanf() does not appear to be returning after the first conversion (matching) failure, you might want to consider using the strtol() or strtoul() functions to convert the integer portions of the strings. These functions provide extra "feedback" about the progress of their conversions.

Children
  • As soon as I read my own question the answer came to me :-) I can detect errors in the input by checking if the c field has been modified by sscanf (since I cannot rely on the return value).
    It will do as a workaround. Clearly there is an error in the implementation of a library function. Hopefully, Keil Software will fix that soon.