-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical12_IndexSequentialFile.cpp
More file actions
188 lines (185 loc) · 3.04 KB
/
Copy pathPractical12_IndexSequentialFile.cpp
File metadata and controls
188 lines (185 loc) · 3.04 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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class Emp
{
int empid;
char name[20];
char design[20];
float salary;
public:
Emp()
{
empid=0;
strcpy(name,“”);
strcpy(design,"");
salary=0.0;
}
int acceptempid()
{
return(empid);
}
void getdata()
{
cout<<"\nEnter Emp details:";
cout<<"\nEnter emp id:";
cin>>empid;
cout<<"Enter emp name : ";
cin>>name;
cout<<"Enter Designation : ";
cin>>design;
cout<<"Enter salary: ";
cin>>salary;
}
void display()
{
cout<<"\n Emp Details";
cout<<"\n Emp id = "<<empid;
cout<<"\n Emp Name = "<<name;
cout<<"\n Designation = "<<design;
cout<<"\n Salary = "<<salary;
}
};
class fileop
{
ifstream fin;
ofstream fout;
fstream fs;
public:
void insert();
void show();
void search(int);
int deleterecord(int);
int append(int);
};
void fileop::insert()
{
Emp obj;
obj.getdata();
fout.open("data.txt",ios::ate|ios::app);
fout.write((char*)&obj,sizeof(obj));
fout.close();
}
void fileop::show()
{
Emp obj;
fin.open("data.txt");
fin.seekg(0,ios::beg);
while(fin.read((char*)&obj,sizeof(obj)))
{
obj.display();
}
fin.close();
}
void fileop::search(int rno)
{
Emp obj;
int flag=0;
fin.open("data.txt");
fin.seekg(0,ios::beg);
while(fin.read((char*)&obj,sizeof(obj)))
{
if(obj. acceptempid()==rno)
{
flag=1;
break;
}
}
fin.close();
if(flag==1)
{
cout<<"\n Emp found";
obj.display();
}
else
{
cout<<"\n Emp not found";
}
}
int fileop::deleterecord(int rno)
{
Emp obj;
int flag=0;
fin.open("data.txt");
fin.seekg(0,ios::beg);
while(fin.read((char*)&obj,sizeof(obj)))
{
if(obj. acceptempid()==rno)
{
flag=1;
}
else
{
fout.write((char*)&obj,sizeof(obj));
}
}
fin.close();
fout.close();
remove("data.txt");
rename("temp.txt","data.txt");
return(flag);
}
int fileop::append(int rno)
{
Emp obj;
int flag=0;
fs.open("data.txt");
fs.seekg(0,ios::beg);
while(fs.read((char*)&obj,sizeof(obj)))
{
if(obj. acceptempid()==rno)
{
flag=1;
cout<<"\n Enter new details: ";
obj.getdata();
fs.seekp((int)fs.tellg()-sizeof(obj),ios::beg);
fs.write((char*)&obj,sizeof(obj));
}
}
fs.close();
return(flag);
}
int main()
{
fileop fobj;
char ch='y';
int choice,key, n;
do{
cout<<"\n Main Menu";
cout<<"\n 1. Create";
cout<<"\n 2. Display";
cout<<"\n 3. Search";
cout<<"\n 4. Delete";
cout<<"\n 5. Append";
cout<<"\n 6. Exit";
cout<<"\n Enter your choice:";
cin>>choice;
switch(choice)
{
case 1: fobj.insert();
break;
case 2: fobj.show();
break;
case 3:
cout<<"\n Enter emp id to search : ";
cin>>n;
fobj.search(n);
break;
case 4:
cout<<"\n Enter emp id to delete : ";
cin>>n;
fobj.deleterecord(n);
break;
case 5:
cout<<"\n Enter emp id to edit : ";
cin>>n;
fobj.append(n);
break;
case 6: exit(0);
}
cout<<"\n Do you want to continue:";
cin>>ch;
}while(ch=='y');
return 0;
}