这是我的代码。我不想使用 scanf()
来读取名字,我尝试使用 fgets()
但是在我输入名字和年龄之后,第二次和第三次我的程序运行不需要 age 的 for 循环。
#include <stdio.h>
#include <string.h>
struct student
{
char name[15];
int age;
};
int main()
{
int i;
struct student s[A];
for (i = 0 ; i < A ; i++)
{
printf("enter the names and age\n");
fgets(s[i].name, 10, stdin);
scanf("%d", &s[i].age);
}
printf("\n\n");
for (i = 0 ; i < A ; i++)
{
printf("%s\t%d\n", s[i].name, s[i].age);
}
return 0;
}
它不起作用,为什么?
但是当我将 fgets 替换为
scanf("%s%d",s[i].name,&s[i].age);
一切正常
最佳答案
The difference between fgets() and scanf()
fgets(...)
通常读取直到收到 '\n'
scanf("%d", ...)
通常:
1. 读取并丢弃前导空白。
2. 读取数字输入(符号、数字)直到扫描到非数字。
3. 非数字被放回到stdin
用于下一个输入函数。
例子:
John回车
"John\n"
被 fgets()
读入 s[0].name
。
21输入
21
被 scanf("%d",...)
读入 s[0].age
。 '\n'
放回stdin
"\n"
被 fgets()
读入 s[1].name
。
M
"M"
被 scanf("%d",...)
读取,s[1].age
中没有任何内容。 'M'
放回 stdin
。
ary输入
"Mary\n"
被 fgets()
读入 s[2].name
。
19输入
19
被 scanf("%d",...)
读入 s[2].age
。 '\n'
放回stdin
"\n"
被 fgets()
读入 s[3].name
。
备选方案:要读取 2 行,调用 fgets()
两次,然后解析:
int Scan_student(struct student *dest) {
char buffer[2][80];
dest->name[0] = '\0';
dest->age[0] = -1;
printf("enter the names and age\n");
for (int i=0; i<2; i++) {
if (fgets(buffer[i], sizeof buffer[i], stdin) == NULL) {
return EOF; // stdin was closed, no more input (or input error)
}
buffer[i][strcspn(buffer[i], "\r\n")] = '\0'; // lop off potential trailing \n
}
// parse the 2 buffers: MANY options here - something simple for now.
if (sscanf(buffer[0], " %14[-'A-Za-z ]", dest->name) != 1) {
return 0;
}
if (sscanf(buffer[1], "%d", &dest->age) != 1) {
return 0;
}
return 1;
}
int i;
struct student st[3];
for (i = 0 ; i < sizeof(st) / sizeof(st[0]) ; i++) {
if (Scan_student(&st[i]) != 1) break;
}
关于c - fgets() 和 scanf() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34371391/