我正在尝试扫描字符串中的单词和数字,如下所示:“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);
关于c - sscanf 直到到达逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16014859/