Read string and various integers in same line in C

so I have a string called buffer which has the following stored: Rb [7, 0] 64
Using sscanf, I’d like to read Rb and store it in a string called name,
read 7 and store it in a int variable called posx, read 0 and store it in a int variable called posy, and read 64 and store it in a int variable called battery_level.

I tried the following, but it doesn’t work:

sscanf(buffer, "%s[^ ] [%d,%d] %d", name, &posx, &posy, &battery_level);


Source: c#

1 Comment

  1. Digin Dominic

    Hope that it helps you.

    I wrote a code, where the inputs will be divided by the first space ‘ ‘ sign, you can use other characters also, and improving this logic you can get your desired result:

    #include
    int main()
    {
    char buffer[] = "Rb [7, 0] 64";
    int posx, posy, batttery_level
    sscanf(buffer, "%[^ ] [%d,%d] %d", name, &posx, &posy, &battery_level);
    printf("%s [%d,%d] %dn", name, posx, posy, battery_level);
    return 0;
    }

    Reply

Leave a Reply