-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass9.cpp
167 lines (162 loc) · 4.05 KB
/
ass9.cpp
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
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include<cstdlib>
#define max 50
using namespace std;
class Employee
{
char name[max];
int empid;
int sal;
char de[50];
friend class FileOperations;
public: Employee()
{
strcpy(name,"");
empid=sal==0;
strcpy(de,"");
}
Employee(char name[max],int empid,int sal,char de[max])
{
strcpy(this->de,de);
strcpy(this->name,name);
this->empid=empid;
this->sal=sal;
}
int getEmpId()
{
return empid;
}
void displayEmployeeData()
{
cout<<endl<<empid<<"\t\t\t"<<name<<"\t\t\t"<<sal<<"\t\t\t"<<de;
}
};
class FileOperations
{
fstream file;
public:FileOperations(char *name)
{
//strcpy(this->name,name);
this->file.open(name,ios::in|ios::out|ios::ate|ios::binary);
}
void insertRecord(int empid,char name[max],int sal,char de[max])
{
Employee s=Employee(name,empid,sal,de);
file.seekp(0,ios::end);
file.write((char*)&s,sizeof(Employee));
file.clear();
}
void displayAllRecords()
{
Employee s;
file.seekg(0,ios::beg);
while(file.read((char *)&s,sizeof(Employee)))
{
s.displayEmployeeData();
}
file.clear();
}
void displayRecord(int empid)
{
Employee s;
file.seekg(0,ios::beg);
void *p;
while(file.read((char *)&s,sizeof(Employee)))
{
if(s.empid==empid)
{
s.displayEmployeeData();
break;
}
}
if(p==NULL)
throw "Element not present";
file.clear();
}
void deleteRecord(int empid)
{
ofstream newFile("new.txt",ios::binary);
file.seekg(0,ios::beg);
bool flag=false;
Employee s;
while(file.read((char *)&s,sizeof(s)))
{
if(s.empid==empid)
{
flag=true;
continue;
}
newFile.write((char *)&s,sizeof(s));
}
if(!flag)
{
cout<<"Element Not Present";
}
file.close();
newFile.close();
remove("Employee.txt");
rename("new.txt","Employee.txt");
file.open("Employee.txt",ios::in|ios::ate|ios::out|ios::binary);
}
~FileOperations()
{
file.close();
cout<<"Closing file..";
}
};
int main()
{
ofstream newFile("Employee.txt",ios::app|ios::binary);
newFile.close();
FileOperations file((char *)"Employee.txt");
int empid,sal,choice=0;
char name[max],de[max];
while(choice!=5)
{
cout<<"\n\n1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Display by RollNo\n";
cout<<"4) Deleting a Record\n";
cout<<"5) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice)
{
case 1 : //New Record
cout<<endl<<"Enter employee id and name : \n";
cin>>empid>>name;
cout<<"Enter sal \n";
cin>>sal;
cout<<"Enter designation : \n";
cin>>de;
file.insertRecord(empid,name,sal,de);
break;
case 2 :
cout<<"Employee ID"<<"\t\t"<<"Name"<<"\t\t"<<"Salary"<<"\t\t"<<"designation\n";
cout<<"----------------------------------------------------------------------";
file.displayAllRecords();
break;
case 3 :
cout<<"Enter employee id";
cin>>empid;
try
{
file.displayRecord(empid);
}
catch(const char *str)
{
cout<<str;
}
break;
case 4:
cout<<"Enter employe id";
cin>>empid;
file.deleteRecord(empid);
break;
case 5 :break;
}
}
}