我有 2 个问题。
这里是 C++ 引用
#include <stdio.h>
int main ()
{
char sentence []="Rudolph is 12 years old";
char str [20];
int i;
sscanf (sentence,"%s %*s %d",str,&i); <---
printf ("%s -> %d\n",str,i);
return 0;
}
问题 1. %*s 到底在做什么?
我的程序正在构建一个哈希表。
它询问用户是否输入
q- quit
i <int> - inserts integer //must be on same line
d <int> - deletes integer //must be on same line
etc....
For example:
in order to insert "35" I would have to type:
i 35
问题 2。由于“q”没有整数,因此 C++ 引用对“q”和“i 35”都适用吗?
char choice[10];
char option;
int i;
sscanf(choice, "%c %d", &option, &i);
如果输入“q”(未附加整数)以及输入“i 35”(附加整数),这是否有效?
最佳答案
这意味着它应该从流中跳过该类型。例如输入流是:
"Rudolph is 12 years old"
第一个%s
将捕获“Rudolph”
,%*s
将“读取但忽略”“is”
(即不将其存储在变量中),然后 %d
将读取并捕获 12
。
我不太清楚你的第二个问题是什么意思。我相信您是在问 "%s %*s %d"
格式是否适用于从输入中读取 "q"
。在这种情况下,您应该使用不同的格式,例如单个字符的 "%s"
或 "%c"
。
针对您更新的问题,您可以轻松尝试一下并查看。在我的测试中,它确实适用于 "q"
的输入:sscanf
单独保留 int
但确实读取了 "q”
。
关于字符数组转整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15938444/