-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
155 lines (145 loc) · 2.89 KB
/
Copy pathfile.cpp
File metadata and controls
155 lines (145 loc) · 2.89 KB
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
148
149
150
151
152
153
154
155
#include<iostream>
#include<fstream>
using namespace std;
class stud
{
private:
char name[10],addr[20],div;
int rno;
public:
void accept()
{
cout<<"\nEnter your name : ";
cin>>name;
cout<<"\nEnter roll number : ";
cin>>rno;
cout<<"\nEnter division : ";
cin>>div;
cout<<"\nEnter address : ";
cin>>addr;
}
void Delete()
{
remove("db.txt");
}
void display();
void update(int rno);
void search(int rno);
void write();
};
void stud::display()
{
stud obj;
ifstream f;
f.open("db.txt",ios::in);
while(f.read((char *)&obj, sizeof(obj)))
{
cout<<"\n---------------------------------------------------------------";
cout<<"\nName : "<<obj.name;
cout<<"\nRoll number : "<<obj.rno;
cout<<"\nDivision : "<<obj.div;
cout<<"\nAddress : "<<obj.addr;
}
f.close();
}
void stud::write()
{
stud obj;
obj.accept();
ofstream f;
f.open("db.txt",ios::out | ios::app);
f.write((char *)&obj, sizeof(obj));
f.close();
}
void stud::search(int r)
{
stud obj;
ifstream f;
f.open("db.txt",ios::in);
int flag=0;
while(f.read((char *)&obj, sizeof(obj)))
{
if(obj.rno==r)
{
flag=1;
break;
}
}
if(flag)
{
cout<<"\n-----------------------!!RECORD FOUND!!-----------------------";
cout<<"\nname: "<<obj.name;
cout<<"\nRoll number: "<<obj.rno;
cout<<"\ndivision: "<<obj.div;
cout<<"\nAddress: "<<obj.addr;
}
else
cout<<"\nRecord not found..!!";
f.close();
}
void stud::update(int r)
{
stud obj;
fstream f;
f.open("db.txt",ios::in | ios::out);
int flag=0;
while(f.read((char *)&obj, sizeof(obj)))
{
if(obj.rno==r)
{
flag=1;
break;
}
}
if(flag)
{
cout<<"\nEnter new data";
obj.accept();
f.seekp(-sizeof(obj),ios::cur);
f.write((char *)&obj, sizeof(obj));
cout<<"\nRECORD Updated..!!";
}
else
cout<<"\nRecord not found..!!";
}
int main()
{
stud my;
int op,r;
char ch;
int exit=0;
do
{
cout<<"\n---------------------------------------------------------------";
cout<<"\nEnter \n1) New data \n2) Display \n3) Search \n4) Update \n5) Delete file\n6] Exit\nchoice = ";
cin>>op;
switch(op)
{
case 1:
my.write();
break;
case 2:
my.display();
break;
case 3:
cout<<"\nEnter roll number to search: ";
cin>>r;
my.search(r);
break;
case 4:
cout<<"\nEnter roll number to update: ";
cin>>r;
my.update(r);
break;
case 5:
my.Delete();
break;
case 6:
exit=1;
break;
default:
cout<<"\nWrong choice..!!";
}
}while(exit==0);
return 0;
}