sscanf直到它达到逗号

我正在尝试扫描字符串中的单词和数字,如下所示:“ hello,world,I,287876,6.0 ”< – 此字符串存储在char数组中(字符串)我需要做的就是将事情分开并将它们分配给不同的变量,这样就好了

  char a = "hello" char b = "world" char c = "I" unsigned long d = 287876 float e = 6.0 

我知道常规scanf在到达空白区域时停止从stdin读取。 所以我一直在想,有可能让sscanf在达到“,” (逗号)时停止阅读

我一直在探索图书馆,找到sscanf的格式,只读字母和数字。 我找不到这样的东西,也许我应该再看一次。

有帮助吗? 提前致谢 :)

如果字符串中变量的顺序是固定的,我的意思是它总是:

 string, string, string, int, float 

sscanf()使用以下格式说明符:

 int len = strlen(str); char a[len]; char b[len]; char c[len]; unsigned long d; float e; sscanf(" %[^,] , %[^,] , %[^,] , %lu , %lf", a, b, c, &d, &e); 

这个使用strtok例子应该会有所帮助:

 #include  #include  int main () { char str[] ="hello, world, I, 287876, 6.0" ; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (str,","); while (pch != NULL) { printf ("%s\n",pch); pch = strtok (NULL, ","); } return 0; } 

请参阅strtok和/或strtok_r的文档