我想使用 sscanf 的宽度选项 ( http://en.cppreference.com/w/c/io/fscanf ) 来控制要读取的最大字节数。但是下面读取了3个多字节。
#include <stdio.h>
int main() {
long long ll;
char str[4];
int pos;
sscanf(" 281", "%3llx%n", &ll, &pos);
printf("%llx %d\n", ll, pos);
sscanf(" 281", "%3s%n", &str, &pos);
printf("%s %d\n", str, pos);
return 0;
}
它打印出以下内容。
281 4
281 4
我还以为它打印出来了
28 3
28 3
这是 sscanf 的正确用法吗? fscanf 的工作方式类似。
最佳答案
一些(大多数)格式隐式跳过空格(基本上除了 %c
和 %[
之外的所有内容)。跳过的空格不计入最大字段宽度。
关于c - sscanf 的宽度选项不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46751986/