-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDATABASE.C
147 lines (145 loc) · 4.21 KB
/
DATABASE.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include<stdio.h>
#include<conio.h>
FILE *fp;
int cd=0,cn=0;
char title[20],aut[20],gen[10],frm[10],dsc[100];
void getdata();
void sreachdata();
void display();
void sortcataog();
struct data{
int cn;
char title[20];
char aut[20];
char gen[10];
char frm[10];
char dsc[30];
}d;
void getdata()
{
printf("Enter Data <c.num> <title> <author> <gener> <format> <discrition> :\n");
scanf("%d%s%s%s%s%s",&d.cn,&d.title,&d.aut,&d.gen,&d.frm,&d.dsc);
//opening file to write data
fp=fopen("db.txt","a");
fprintf(fp,"%d %s %s %s %s %s\n",d.cn,d.title,d.aut,d.gen,d.frm,d.dsc);
fclose(fp);
}
void searchdata()
{
int scn,c=0;
printf("Enter Item Catalog no to srearch : ");
scanf("%d",&scn);
fp=fopen("db.txt","r");
fseek(fp,0,SEEK_END);
cd=ftell(fp); //getting the last position of filpointer in cd
fseek(fp,0,SEEK_SET);
printf("Cat.Num\tTitle\t\tAuthor\tGenre\tFormat\tDiscroption");
printf("\n---------------------------------------------------");
while(ftell(fp)!=cd)
{
fscanf(fp,"%d %s %s %s %s %s",&cn,&title,&aut,&gen,&frm,&dsc);
if(cn==scn)
{
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",cn,title,aut,gen,frm,dsc);
c=1;
}
}
if(c==0) printf("NOT FOUND !!!");
fclose(fp);
}
void display()
{
fp=fopen("db.txt","r");
fseek(fp,0,SEEK_END);
cd=ftell(fp);
fseek(fp,0,SEEK_SET);
printf("\n\nALL CATALOG ITEM :\n\n");
printf("Cat.Num\tTitle\t\tAuthor\tGenre\tFormat\tDiscroption");
printf("\n------------------------------------------------------------");
while(ftell(fp)!=cd)
{ fscanf(fp,"%d %s %s %s %s %s",&cn,&title,&aut,&gen,&frm,&dsc);
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",cn,title,aut,gen,frm,dsc);
}
fclose(fp);
}
void sortcatalog()
{
char ch;
int c=0,i=0;
struct data *ds;
struct data *dn;
fp=fopen("db.txt","r");
ch=getc(fp);
while(ch!=EOF)
{
if(ch=='\n')
c++;
ch=getc(fp);
}
fclose(fp);
ds=(struct data*)malloc(c*sizeof(struct data));
dn=ds;
fp=fopen("db.txt","r");
for(i=0;i<c;i++)
{ fscanf(fp,"%d %s %s %s %s %s",&ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
printf("Cat.Num\tTitle\t\tAuthor\tGenre\tFormat\tDiscroption");
printf("\n---------------------------------------------------------------");
for(i=0;i<c;i++)
{ if(strcmp("book",ds->frm)==0)
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
for(i=0;i<c;i++)
{ if(strcmp("cd",ds->frm)==0)
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
for(i=0;i<c;i++)
{ if(strcmp("dvd",ds->frm)==0)
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
for(i=0;i<c;i++)
{ if(strcmp("ebook",ds->frm)==0)
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
printf("\n\nList of items that fall behind the known format:\n");
for(i=0;i<c;i++)
{ if(strcmp("book",ds->frm)&&strcmp("cd",ds->frm)&&strcmp("dvd",ds->frm)&&strcmp("ebook",ds->frm)!=0)
printf("\n%d\t%s\t\t%s\t%s\t%s\t%s",ds->cn,ds->title,ds->aut,ds->gen,ds->frm,ds->dsc);
ds++;
}
ds=dn;
}
void main()
{
int ch=0;
clrscr();
menu:printf("\n\n******MAIN MENU*******");
printf("\n1. ADD\n2. DISPLAY ITEM\n3. DISAPLAY CATALOG\n4. DISPLAY SORTED CATALOG\n5. EXIT\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1: getdata();
goto menu;
case 2: searchdata();
goto menu;
case 3: display();
goto menu;
case 4: sortcatalog();
goto menu;
case 5: goto z;
default: printf("\n\n****WRONG CHOICE TRY AGAIN***");
goto menu;
}
z:printf("\n\n****PRESS ANY KEY TO EXIT !!******");
getch();
}