【发布时间】:2021-01-08 15:23:19
【问题描述】:
我的 sscanf 函数有问题...
这是我的代码:
char str[]="Andrew;;;3454";
char name[20] = {0};
char city[20] = {0};
char age[5] = {0};
char hasDegree[20] = {0};
sscanf(str,"%[^;];%[^;];%[^;];%[^;]",name,city,age,hasDegree);
printf("%s is %s Years Old and live in %s at %s degrees",name,age,city,hasDegree);
输出:Andrew is Years Old and live in at degrees
如您所见,打印的是“Andrew”但不是“3454”(因为之前有2个空白?)
请问如何解决?
我自愿用 char 声明了所有内容:)
谢谢!
【问题讨论】:
-
如果您需要任何健壮性解析数据,请不要使用任何版本的
scanf()。 -
你建议我现在用什么?我听说过 strtok
-
我使用了 strsep,当我打印时,它可以工作!但是如何将每个分隔的字符串保存在变量中? (姓名、城市等)我想使用 typedef 结构,例如: typedef struct identity{char name[20].......}identity;