Project On Data File Handling

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

1

2
1

PROJECT ON

DATA FILE HANDLING

PROJECT COMPILED BY
KUMAR LAL

3
4

ROLL NO

: 15

STANDARD

: XII A

RAKESH

5
6
3
4

7
8

9
10
5#include<iostream.h>
6#include<conio.h>
7#include<fstream.h>
8#include<stdio.h>
9#include<stdlib.h>
10#include<string.h>
11#include<process.h>
12class rest
13{private:
14

float price;

15

int prqnty;

16
17 public:

int prcode;

18

char prname[50];

19

void display()

20

{cout<<"\n\n";

21

cout<<"\n* Product code

22

cout<<"\n* Product name

23

cout<<"\n* Product price

24

cout<<"\n* Product quantity

25

26

void store()

27

{cout<<"\n\n";

:"<<prcode;
:"<<prname;
:"<<price;
:"<<prqnty;

28

cout<<"\nEnter product code

29

cout<<"\nEnter product name

30

cout<<"\nEnter product quantity :";cin>>prqnty;

11
12

:";cin>>prcode;
:";cin>>prname;

13
14
31
32

cout<<"\nEnter product price

:";cin>>price;

33};
34
35void admin_menu();
36void admin();
37void main_menu();
38void searchrec();
39void searchrec(char abc);
40
41void storerec()
42{ofstream file1("resta.dat",ios::binary);
43 rest r1;clrscr();
44 char ans='y';
45 for(int i=0;i<80;i++)
46 cout<<"=";
47 cout<<"\n\t\t\t\tCREATION WINDOW\n\t\t\t\t_______________";
48 do
49 {r1.store();
50 file1.write((char*)&r1,sizeof(r1));
51 cout<<"Press y to continue\n";cin>>ans;
52 }while(ans=='y'||ans=='Y');
53file1.close();
54}
55
56void disprec()

15
16

17
18
57{ifstream file1("resta.dat",ios::binary);
58 rest r1;clrscr();
59 cout<<"\n";
60 for(int i=0;i<80;i++)
61 cout<<"=";
62 cout<<"\n\t\t\t\tDISPLAY WINDOW\n\t\t\t\t______________";
63 file1.read((char*)&r1,sizeof(r1));
64 while(!file1.eof())
65 {r1.display();
66 cout<<"\npress any key for next record";
67 file1.read((char*)&r1,sizeof(r1));getch();cout<<endl;
68 if(file1.tellg()==sizeof(r1)-1)break;
69 }file1.close();getch(); main_menu();
70}
71
72void disprecin()
73{ifstream file1("resta.dat",ios::binary);
74 rest r1;clrscr();
75 cout<<"\n";
76 for(int i=0;i<80;i++)
77 cout<<"=";
78 cout<<"\n\t\t\t\tDISPLAY WINDOW\n\t\t\t\t______________";
79 file1.read((char*)&r1,sizeof(r1));
80 while(!file1.eof())
81 {r1.display();
82 cout<<"\npress any key for next record";

19
20

21
22
83 file1.read((char*)&r1,sizeof(r1));getch();cout<<endl;
84 if(file1.tellg()==sizeof(r1)-1)break;
85 }file1.close();getch();
86}
87
88void searchrec()
89{rest r1;char code[20],ch;
90 do
91 {int f=0;clrscr();
92 ifstream file1("resta.dat",ios::binary);
93 cout<<"\n";
94 for(int i=0;i<80;i++)
95 cout<<"=";
96 cout<<"\n\t\t\t\tSEARCH WINDOW\n\t\t\t\t_____________";
97 cout<<"\n\n\n\t\t\t Enter product name:";gets(code);
98 while(!file1.eof())
99 {file1.read((char*)&r1,sizeof(r1));
100 if(strncmp(code,r1.prname,3)==0)
101 {r1.display();f=1;}
102 }
103 if(f==0)
104 {cout<<"\n\n\n\t\t\t
record not present";}file1.close();cout<<"\n\n\t\t
105to search again or N to go back ";ch=getche();
106 }while(ch!='n'); main_menu();
107}
108
109void searchrec(char code[])
23
24

press Y

25
26
110{rest r1;char ch;
111
112 {int f=0;clrscr();
113 ifstream file1("resta.dat",ios::binary);
114 for(int i=0;i<80;i++)
115 cout<<"=";
116 cout<<"\n\t\t\t\tSEARCH WINDOW\n\t\t\t\t_____________";
117 cout<<"\n\t\t\t

Product information";

118 while(!file1.eof())
119 {file1.read((char*)&r1,sizeof(r1));
120 if(strncmp(code,r1.prname,3)==0)
121 {r1.display();f=1;}
122 }
123 if(f==0)
124 {cout<<"\n\n\n\t\t\t
record not present";}file1.close();cout<<"\n\n\t\t
125to search again or N to go back ";ch=getche();
126 if(ch=='y')searchrec();
127 else if(ch=='n')main_menu();
128 }
129}
130void delrec()
131{ifstream file1("resta.dat",ios::binary);
132 ofstream file2("temp.dat",ios::binary);
133 int code,f=0;rest r1;clrscr();
134 cout<<"\n\t\t\t\tDELETION WINDOW\n\t\t\t\t_______________";
135 cout<<"\n\nEnter product code to del:";cin>>code;
136 while(!file1.eof())
27
28

press Y

29
30
137 {file1.read((char*)&r1,sizeof(r1));
138 if(code==r1.prcode)
139 {file2.write((char*)&r1,sizeof(r1));}
140 else f=1;
141 }
142 file1.close();file2.close();
143 remove("resta.dat");
144 rename("temp.dat","stud.dat");
145 if(f==1)
146 cout<<"rec found and deleted:";
147 else cout<<"rec not present:";
148}
149
150void modifyrec()
151{fstream file1("stud.dat",ios::binary|ios::in|ios::out);
152 rest r1;clrscr();
153 int code,f=0,pos=-1;
154 cout<<"\n\t\t\t\tMODIFICATION WINDOW\n\t\t\t\t___________________";
155 cout<<"\n\nEnter code to modify:";cin>>code;
156 while(!file1.eof())
157 {file1.read((char*)&r1,sizeof(r1));
158 if(code==r1.prcode)
159 {f=1;
160 pos=(file1.tellg()-sizeof(r1));
161 break;
162 }

31
32

33
34
163 }
164 if(f==1)
165 {file1.seekp(pos);cout<<"\nEnter new values\n";r1.store();
166 file1.write((char*)&r1,sizeof(r1));
167 }
168 else cout<<"\nno record present";
169 file1.close();
170}
171
172
173int password()
174{clrscr();char pass[20];int per;
175 cout<<"\n\n\t\t

Enter admin password //rake71";

176 cout<<"\n\n\t\t\tEnter password\n\n\t\t\t ";gets(pass);


177 if(strcmp(pass,"rake71")==0)
178 per=1;
179 else per=0;
180 return(per);
181}
182
183void admin()
184{clrscr();char ans;
185 if(password()==1)
186 admin_menu();
187 else
188 {cout<<"\nsorry incorrect password\nEnter\n1.try again\n2.go back";

35
36

37
38
189 ans=getche();if(ans!='1'&&ans!='2')ans='0';
190 switch(ans)
191 {case '1':admin();break;
192 case '2':main_menu();break;
193 case '0':cout<<"\n\ninvalid input";getch();admin();
194 }
195 }
196}
197void admin_menu()
198{char ans;clrscr();
199 cout<<"\n\t\t\t\tADMIN MENU\n\t\t\t\t__________";
200 cout<<"\n\nEnter 1-6\n\n1.create record\n2.display all records\n3.search
201record\n4.modify record\n5.delete record\n6.go back"<<endl;
202 ans=getche();if(ans!='1'&&ans!='2'&&ans!='3'&&ans!='4'&&ans!='5'&&ans!
203='6')ans='0';
204 do
205 {switch(ans)
206 {case '1':storerec(); admin_menu();
207 case '2':disprecin(); admin_menu();
208 case '3':searchrec(); admin_menu();
209 case '4':modifyrec(); admin_menu();
210 case '5':delrec();

admin_menu();

211 case '6':main_menu(); admin_menu();


212 case '0':cout<<"\n\ninvalid input";getch();admin_menu();
213 }}while(ans!=6);
214}
215
39
40

10

41
42
216void main_menu()
217{clrscr();char ans[20],ch;
218 cout<<"\n\t\t\t\tMAIN MENU\n\t\t\t\t_________";
219 gotoxy(9,25);cout<<"1.admin";
220 gotoxy(32,25);cout<<"2.display all rec";
221 gotoxy(65,25);cout<<"3.exit";
222 gotoxy(30,3);cout<<"\n\n\n\t\t\tEnter product name:";gets(ans);
223 if(ans[0]=='1'||ans[0]=='2'||ans[0]=='3')
224 {ch=ans[0];
225
226 switch(ch)
227 {case '1':admin();break;
228 case '2':disprec();break;
229 case '3':exit(0);
230 }}
231
232 else searchrec(ans);
233}
234
235void main()
236{clrscr();
237 ifstream file1("xyz.txt");
238 char word[200];
239 while(!file1.eof())
240 {file1.getline(word,100,'\n');
241 cout<<word<<"\n";

43
44

11

45
46
242 }
243 getch();
244 ifstream file2("abc.txt");
245 while(!file2.eof())
246 {file2.getline(word,100,'\n');
247 cout<<word<<"\n";
248 }getch();
249 main_menu();
250 getch();
251}

47
48

12

49
50
252
253

51
52

13

You might also like