Use sscanf to read multiple times string

前端 未结 1 1540
心在旅途
心在旅途 2021-01-19 16:36

I\'m trying to read the content of a string, inside a multidimensional array...the problem is, when I do this, the sscanf continues reading only the first character only...<

相关标签:
1条回答
  • 2021-01-19 17:26

    Use the %n specifier in the format string.

    E.g.

    #include <stdio.h>
    
    int main(void){
        const char *str="A1+A2+A3+A4.";
        char col;
        int line;
        int offset = 0, readCharCount;
        while(sscanf(str + offset, "%c%d%*c%n", &col, &line, &readCharCount)==2){
            printf("%c, %d\n", col, line);
            offset += readCharCount;
        }
        return 0;
    }
    /* result
    A, 1
    A, 2
    A, 3
    A, 4
    */
    
    0 讨论(0)
提交回复
热议问题