Iterating with sscanf

后端 未结 4 421
闹比i
闹比i 2021-01-29 08:58

I do not know much about the sscanf function, but what I am trying to do is iterate through a line of integers. Given the variable

    char *lineOfInts
         


        
4条回答
  •  长情又很酷
    2021-01-29 09:16

    Use "%n" to record the count of characters scanned.

    char *lineOfInts;
    
    char *p = lineOfInts;
    while (*p) {
      int n;
      int number;
      if (sscanf(p, "%d %n", &number, &n) == 0) {
        // non-numeric input
        break;
      }
      p += n;
      printf("Number: %d\n", number);
    }
    

提交回复
热议问题