#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void add();
void search();
void update();
struct number
{
char name[20];
char phnum[15];
} n;
int main(void)
{
int choice;
printf("1.Add\n2.Search\n3.Update\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
add();
break;
case 2:
search();
break;
case 3:
update();
break;
}
}
void add()
{
// struct number n;
FILE *filepointer;
filepointer = fopen("diary.txt", "a+");
printf("Enter The Phone Number (country code + 10digits) :");
scanf("%s", &n.phnum);
// printf("%s", n.phnum);
fflush(stdin);
printf("Choose The Contact Name :");
scanf("%[^\n]s", strlwr(n.name));
fwrite(&n, sizeof(n), 1, filepointer);
fclose(filepointer);
printf("\nSuccessfull Adding To File\n\n");
printf("Press \'A\' to continue adding , \'S\' to search Contact ,\'U\' to update Contact, esc to close ");
char ch = getch();
if (ch == 'A' || ch == 'a')
{
system("cls");
add();
}
if (ch == 'u' || ch == 'U')
{
system("cls");
update();
}
if (ch == 's' || ch == 'S')
{
system("cls");
search();
}
}
void search()
{
char searchname[50];
FILE *p;
p = fopen("diary.txt", "r");
fflush(stdin);
printf("Enter Contact Name To Search :");
scanf("%[^\n]s", strlwr(searchname));
while (fread(&n, sizeof(n), 1, p))
{
if (strcmp(searchname, n.name) == 0)
{
printf("\n---------------\n%s\n%s\n---------------\n", strupr(n.name), n.phnum);
}
else
{
printf("No Data Found");
fflush(stdin);
printf("\nPress Y to Search Again And esc to exit ");
char ch = getch();
if (ch == 'y' || ch == 'Y')
{
search();
}
}
printf("Press \'A\' to continue adding ,\'U\' to update Contact, esc to close \n");
char ch = getch();
if (ch == 'A' || ch == 'a')
{
system("cls");
add();
}
if (ch == 'u' || ch == 'U')
{
system("cls");
update();
}
if (ch == 's' || ch == 'S')
{
system("cls");
search();
}
}
fclose(p);
}
void update()
{
FILE *p;
char updatename[20];
long int size = sizeof(n);
p = fopen("diary.txt", "r+");
fflush(stdin);
printf("Enter Contact Name To Update : ");
scanf("%[^\n]s", strlwr(updatename));
while (fread(&n, sizeof(n), 1, p) == 1)
{
if (strcmp(n.name, updatename) == 0)
{
printf("\nEnter New Phone Number:");
scanf("%s", &n.phnum);
fseek(p, -size, SEEK_CUR); //points current position in file
fwrite(&n, sizeof(n), 1, p);
break; //imppppppp
}
}
printf("\nSuccessfull updated New Number To File\n\n");
printf("Press \'A\' to continue adding , \'S\' to search Contact ,\'U\' to update Contact, esc to close ");
char ch = getch();
if (ch == 'A' || ch == 'a')
{
system("cls");
add();
}
if (ch == 'u' || ch == 'U')
{
system("cls");
update();
}
if (ch == 's' || ch == 'S')
{
system("cls");
search();
}
fclose(p);
}
0 comments:
Post a Comment