#include <stdio.h>
#include <string.h>
FILE *f;
struct record
{
char name[20];
char tel_num[20];
} rec;
void append()
{
f = fopen("records.dat","a");
printf("Enter the name: ");
scanf("%s",rec.name);
printf("Enter the telephone: ");
scanf("%s",rec.tel_num);
fwrite(&rec,sizeof(rec),1,f);
fclose(f);
}
int display()
{
char name[20];
f = fopen("records.dat","r");
printf("Enter the name to find: ");
scanf("%s",name);
while (!feof(f))
{
fread(&rec,sizeof(rec),1,f);
if (strcmp(rec.name,name) == 0)
{
printf("Telefone number: %s\n",rec.tel_num);
fclose(f);
return 0;
}
}
printf("\nRecord not found!\n");
fclose(f);
return 0;
}
void menu()
{
printf("\n");
printf("1.Append record\n");
printf("2.Display record\n");
printf("3.Exit\n");
}
int main()
{
char ch;
menu();
while ((ch = getchar()) != '3')
{
switch(ch)
{
case '1': { append(); menu(); break;}
case '2': { display(); menu(); break;}
}
}
return 0;
}
Comments