-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_13.cpp
More file actions
204 lines (202 loc) · 4.01 KB
/
assignment_13.cpp
File metadata and controls
204 lines (202 loc) · 4.01 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class Student
{
public:
int rno;
int n, ch, flag, recno;
char nm[20],name[20],addr[20],div;
void read();
void display();
void formatting();
void insert();
void show();
void search();
void modify();
void removerec();
void disprec();
};
void Student::read()
{
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Roll Number: ";
cin>>rno;
cout<<"Enter Division: ";
cin>>div;
cout<<"Enter address: ";
cin>>addr;
}
void Student::display()
{
cout<<"\n| \t"<<rno<<"\t\t|\t"<<name <<"\t\t| \t"<<div<<"\t\t| \t"<<addr<<"\t\t|";
}
void Student::formatting()
{
cout<<"\t\t\t\t\tStudent Directory";
cout<<"\n";
cout<<"\n|\tR.No. \t\t| \tName \t\t| \tDivision \t| \tAddress \t|";
}
void Student::insert()
{
Student t;
fstream file;
file.open("Student.txt", ios::app|ios::out);
cout<<"Enter the total number of records you want to enter: ";
cin>>n;
for(int i=0;i<n;i++)
{
t.read();
file.write((char *)&t, sizeof(t));
}
file.close();
}
void Student::show()
{
Student t;
fstream file;
t.formatting();
file.open("Student.txt", ios::in|ios::out);
while(file.read((char *)&t,sizeof(t)))
t.display();
file.close();
}
void Student::search()
{
Student t;
fstream file;
flag=0;
cout<<"Enter the name you want to find: ";
cin>>nm;
flag=0;
t.formatting();
file.open("Student.txt", ios::in|ios::out);
while(file.read((char *)&t,sizeof(t)))
{
if(strcmp(t.name,nm)==0)
{
flag=1;
t.display();
break;
}
}
file.close();
if(flag==0)
cout<<"\n|\t\tRecord not found\t\t|";
cout<<"\n+-----------------------------------------------------------------------------------------------+\n";
}
void Student::modify()
{
Student t;
fstream file;
flag=0;
cout<<"Enter the name whose entry you want to modify: ";
cin>>nm;
flag=0;
file.open("Student.txt", ios::in|ios::out);
while(file.read((char *)&t,sizeof(t)))
{
if(strcmp(t.name,nm)==0)
{
flag=1;
cout<<"Record found\nEnter modified information"<<endl;
Student tp;
tp.read();
file.seekp(-1*(sizeof(t)),ios::cur);
file.write((char *)&tp, sizeof(t));
break;
}
}
file.close();
if(flag==0)
cout<<"Record not found";
}
void Student::removerec()
{
Student t;
fstream file;
flag=0;
cout<<"Enter the name whose entry you want to delete: ";
cin>>nm;
fstream tfile;
file.open("Student.txt",ios::in|ios::out);
while(file.read((char *)&t,sizeof(t)))
{
if(strcmp(t.name,nm)==0)
{
flag=1;
break;
}
}
file.close();
if(flag==0)
cout<<"Record not found\n";
else
{
tfile.open("Temp.txt",ios::out);
file.open("Student.txt",ios::in);
while(file.read((char *)&t,sizeof(t)))
{
if(strcmp(t.name,nm)!=0)
{
tfile.write((char *)&t,sizeof(t));
}
}
file.close();
tfile.close();
remove("Student.txt");
rename("Temp.txt","Student.txt");
cout<<"Record deleted successfully\n";
}
}
void Student::disprec()
{
Student t;
fstream file;
flag=0;
cout<<"Enter the record number you want to display: ";
cin>>recno;
file.open("Student.txt", ios::in|ios::out);
if(file.eof())
file.clear();
file.seekp((recno-1)*(sizeof(t)));
file.read((char *)&t,sizeof(t));
t.formatting();
t.display();
cout<<"\n+-----------------------------------------------------------------------------------------------+\n";
file.close();
if(flag==0)
cout<<"Record not found";
}
int main()
{
int ch;
char c;
cout<<"Options: \n1.Insert\n2.Display\n3.Search\n4.Modify\n5.Delete\n6.Display Specific Record\n";
Student t;
do
{
cout<<"\nEnter choice: ";
cin>>ch;
switch(ch)
{
case 1: t.insert();
break;
case 2: t.show();
break;
case 3: t.search();
break;
case 4: t.modify();
break;
case 5: t.removerec();
break;
case 6: t.disprec();
break;
}
cout<<"If you wan to continue press 'y', if not then press 'n': ";
cin>>c;
}while(c=='y'||c=='Y');
return 0;
}