fgets 不复制第一个字母
fgets not copying first letter
我写了这段代码:
check_name = 0;
printf("Enter the Location of the photo:\n");
fgets(location, MAX_LOCATION_SIZE, stdin);
printf("Enter the name of the photo:\n");
count = 0;
while (check_name == 0)
{
if (count > 0)
{
printf("The name is already in the list!\nChoose another one:\n"); // will not do it only for the first time
}
getchar;
fgets(name, MAX_NAME_SIZE, stdin);
check_name = checkName(frameList, name);
count++;
}
现在我来解释一下什么是一切:
我做了 check_name
因为我正在做一个测试来检查这个名字是否已经在列表中(这个问题并不重要所以我不会上传代码除非你真的需要它)
我把 getchar
放在 fgets
之前,因为没有它,它甚至不需要名字。
每次我输入内容到 fgets(entering a name)
它没有复制我的第一个字母(如果我把 Dexter 放在它的 saving exter 中)并且我已经检查过我是否给了它足够的位置(char name[50]
- 我的老师告诉我这将是最大的)
getchar() 将接收一个字符,其余字符可供 fgets() 使用。所以删除它。
如果您第一次丢失一个字符时遇到问题...将 getchar() 放在循环之前。甚至你可以写 scanf("%*c");
我写了这段代码:
check_name = 0;
printf("Enter the Location of the photo:\n");
fgets(location, MAX_LOCATION_SIZE, stdin);
printf("Enter the name of the photo:\n");
count = 0;
while (check_name == 0)
{
if (count > 0)
{
printf("The name is already in the list!\nChoose another one:\n"); // will not do it only for the first time
}
getchar;
fgets(name, MAX_NAME_SIZE, stdin);
check_name = checkName(frameList, name);
count++;
}
现在我来解释一下什么是一切:
我做了 check_name
因为我正在做一个测试来检查这个名字是否已经在列表中(这个问题并不重要所以我不会上传代码除非你真的需要它)
我把 getchar
放在 fgets
之前,因为没有它,它甚至不需要名字。
每次我输入内容到 fgets(entering a name)
它没有复制我的第一个字母(如果我把 Dexter 放在它的 saving exter 中)并且我已经检查过我是否给了它足够的位置(char name[50]
- 我的老师告诉我这将是最大的)
getchar() 将接收一个字符,其余字符可供 fgets() 使用。所以删除它。
如果您第一次丢失一个字符时遇到问题...将 getchar() 放在循环之前。甚至你可以写 scanf("%*c");