Iterating with sscanf

后端 未结 4 422
闹比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:29

    Maybe something like that :

    #include 
    
    int main(void)
    {
      const char * str = "10 202 3215 1";
      int i = 0;
      unsigned int count = 0, tmp = 0;
      printf("%s\n", str);
      while (sscanf(&str[count], "%d %n", &i, &tmp) != EOF) {
        count += tmp;
        printf("number %d\n", i);
      }
    
      return 0;
    }  
    

提交回复
热议问题