Whenever I run this code, final scanf function to get 'email' input does not execute and I get 'Updated successfully!' message directly! I tried using gets()
instead of scanf and I'm getting the same problem. Can someone please explain the problem to me?
#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();
}