-
Notifications
You must be signed in to change notification settings - Fork 0
/
directory.c
85 lines (75 loc) · 1.43 KB
/
directory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct CONTACTS
{
int num;
char name[50];
};
int main(void)
{
struct CONTACTS contact[5];
FILE *file;
int NUM,i,j;
char NAME[50],NAME_S[50];
int exist=false,valid=true;
for(i=0;i<5;i++)
{
printf("Enter the %i Contact Name : ",i+1);
scanf("%s",&NAME);
do
{
printf("Enter the %i Contact Num : ",i+1);
if(scanf("%i",&NUM)!=1)
{
printf("Please Enter the Valid Contact Number!!\n");
while(getchar()!='\n');
valid = false;
}
else
valid = true;
}while(!valid);
for(j=0;j<=i;j++)
{
if(strcmp(contact[j].name, NAME) == 0)
{
printf("This Contact is already exist..\n");
exist =true;
i--;
break;
}
}
if(!exist)
{
contact[i].num = NUM;
strcpy(contact[i].name, NAME);
}
exist = false;
}
file = fopen("Directory.dat","w+");
for(i=0;i<5;i++)
{
fprintf(file,"%i\t%s\n",contact[i].num,contact[i].name);
}
fclose(file);
printf("\n-----------------------------------------------------------------------\n");
while(true)
{
valid= false;
printf("Enter the Contact Name :");
scanf("%s",&NAME);
file = fopen("Directory.dat","r+");
while(fscanf(file,"%i\t%s",&NUM,&NAME_S)!=EOF)
{
if(strcmp(NAME,NAME_S)==0)
{
printf("Contact Num : %i\n\n",NUM);
valid = true;
break;
}
}
fclose(file);
if(!valid)
printf("This Contact is dosnt exist...\n\n");
}
}