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

Problem using sscanf with \0

hello,

I'm creating a parser to parse through data from UART. I managed to get it working with VS code and gcc but had a hard time implementing it in my initial keil project. Here's my code:

typedef struct device
{
        int n;
        char MAC[12];
        char name[20];
}device;

void readlines(char *str,device* t) {
        const char s[5] = "\r\n";
        char *token;
        int i = 0;

        token = strtok(str, s);
        while (token != NULL) {
                sscanf(token, "OK+DIS%d:%12cOK+NAME:%[^\0]\r\n", &t[i].n, t[i].MAC,  t[i].name);
                token = strtok(NULL, s);
                i++;
                }
}


%[^\0] is meant to include spaces in the t[i].name since sscanf consider \0 as a delimiter to the message, and %[^ ] doesn't work as intended.
But I get always the same error unless I change %[^\0] by %[^ ] or something else. I have the <stdio.h> included by the way .Here's the error message:
Error: L6218E: Undefined symbol _scanf_wctomb (referred from application.o).

Best regards.

  • %[^\0] is meant to include spaces in the t[i].name since sscanf consider \0 as a delimiter to the message,

    No. '\0' delimits the format string, like it would any string in a C program. sscanf never sees it. So your input format is actually "%[^", which makes no sense at all. I suspect it's bad enough to qualify for the ultimate harsh judgement by the C standard: "causes undefined behaviour". As such, a linker error is actually one of the nicer result you could have got.

    and %[^ ] doesn't work as intended.

    That's because it expresses the exact opposite of what you intended: it includes everything except for spaces.