为什么要以argv调用的sscanf()[]只能使用一次? [英] Why call sscanf() with argv[] can only use once?
本文介绍了为什么要以argv调用的sscanf()[]只能使用一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要的argv [1]和argv [2]不同的类型。我发现我只能用的sscanf()一次或argv中的下一个字符串无法检索。
这里是我的code。
I need to get argv[1] and argv[2] to different types. I found that I could only use sscanf() once or the next string in argv cannot be retrieved. Here's my code.
int main( int argc, char *argv[])
{
char t;
float temp;
sscanf(argv[1], "-%[cf]",&t);
sscanf(argv[2], "%f", &temp);
return 0;
}
只有前的sscanf()可以得到格式化的值。
我怎么可能还得到以argv完成[2]?
Only the first sscanf() can get the formatted value. How could I also get done with argv[2]?
推荐答案
试图保存在字符
导致未定义行为(UB)字符串数据。
Attempt to save string data in a char
leading to undefined behavior (UB).
%[]
期望匹配的字符数组。
"%[]"
expects to match a character array.
// char t;
// sscanf(argv[1], "-%[cf]",&t);
char t[100];
if (sscanf(argv[1], "-%99[cf]",t) != 1) Handle_Failure();
推荐:结果
添加的宽度限制,比如99,来限制字符串输入。设置为1小于大小 T
。结果
检查返回值的sscanf()
。
这篇关于为什么要以argv调用的sscanf()[]只能使用一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文