每当我运行此代码时,获取“电子邮件”输入的最终 scanf 函数都不会执行,我会得到“更新成功!” 直接留言!我尝试使用gets()
而不是 scanf 并且我遇到了同样的问题。有人可以向我解释这个问题吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct Directory
{
char name[20], email[20];
long int phone;
}Directory;
void add()
{
Directory d;
FILE *file;
file = fopen("phonebook.bin", "ab");
if(!file)
printf("Failed to open file!");
else
{
printf("Enter the name: ");
scanf("%[^\n]", &d.name);
printf("Enter the Phone Number: ");
scanf("%ld", &d.phone);
printf("Enter the e-mail ID: ");
scanf("%[^\n]", &d.email);
if(fwrite(&d, sizeof(Directory), 1, file))
printf("Updated successfully!");
else
printf("Something went wrong, Please try again!");
}
fclose(file);
}
int main()
{
add();
}