- 始终检查来自
sscanf()
及其亲属的返回值。
- 启用编译器警告。一个好的编译器会告诉你下一个问题:
- 当您希望读取姓氏时,不要使用
*
in禁止分配。%*[0-9]
请注意,您的sscanf()
行不使用您提到的数据结构。但是,此示例代码可以:
#include <stdio.h>
typedef struct who
{
char name[20];
char surname[20];
int age;
char gender[7];
} who;
int main(void)
{
const char *data[2] =
{
"Pebbles Flintstone4Female",
"Bam-Bam Rubble3Male",
};
const char *fmt[2] =
{
"%[^ ]%*[^1234567890]%d%s",
"%[^ ]%[^1234567890]%d%s",
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
who buff;
int n;
if ((n = sscanf(data[j], fmt[i], buff.name, buff.surname, &buff.age, buff.gender)) != 4)
printf("Oops: format \"%s\", n = %d: %s\n", fmt[i], n, data[j]);
else
printf("Format \"%s\": data %s: %s %s %d %s\n",
fmt[i], data[j], buff.name, buff.surname, buff.age, buff.gender);
}
}
return 0;
}
样本输出:
Oops: format "%[^ ]%*[^1234567890]%d%s", n = 3: Pebbles Flintstone4Female
Oops: format "%[^ ]%*[^1234567890]%d%s", n = 3: Bam-Bam Rubble3Male
Format "%[^ ]%[^1234567890]%d%s": data Pebbles Flintstone4Female: Pebbles Flintstone 4 Female
Format "%[^ ]%[^1234567890]%d%s": data Bam-Bam Rubble3Male: Bam-Bam Rubble 3 Male
如果您使用该sscanf()
格式的字符串文字进行编译,GCC 会警告您该问题:
td.c: In function ‘main’:
td.c:23: warning: format ‘%d’ expects type ‘int *’, but argument 4 has type ‘char *’
td.c:23: warning: format ‘%s’ expects type ‘char *’, but argument 5 has type ‘int *’
td.c:23: warning: too many arguments for format
上面带有不同格式字符串的代码不能给你那个警告。
也应该修改格式字符串以避免缓冲区溢出:
"%19[^ ] %19[^0-9] %d %6s"