-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingWithFiles.cpp
More file actions
127 lines (122 loc) · 2.53 KB
/
WorkingWithFiles.cpp
File metadata and controls
127 lines (122 loc) · 2.53 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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class employee
{
int empno;
char name[23];
float salary;
public :
void getdata();
void putdata();
int getempno();
};
int employee::getempno()
{
return empno;
}
void employee::getdata()
{
cout<<"Employee number : ";
cin>>empno;
fflush(stdin);
cout<<"Employee name : ";
gets(name);
fflush(stdin);
cout<<"Salary : ";
cin>>salary;
}
void employee::putdata()
{
cout<<"Employee number : "<<empno<<"\n";
cout<<"Employee name : ";
puts(name);
//cout<<"\n";
cout<<"Salary : "<<salary;
}
void newemployee()
{
fstream f1("employee.dat",ios::binary|ios::out|ios::app);
employee emp;
emp.getdata();
f1.write((char *)&emp,sizeof(emp));
f1.close();
}
void display()
{
fstream f1("employee.dat",ios::binary|ios::in);
employee emp;
f1.read((char *)&emp,sizeof(emp));
emp.putdata();
f1.close();
}
void del()
{
fstream f1("employee.dat",ios::binary|ios::in);
fstream f2("temporary.dat",ios::binary|ios::out);
employee emp;
f1.seekg(0,ios::beg);
int eno,fnd;
char confirm='y';
cout<<"Empno to delete :";
cin>>eno;
fnd=0;
while(f1.read((char *)&emp,sizeof(emp)))
{
if(eno==emp.getempno())
{
emp.putdata();
fnd=1;
cout<<"Delete ?";
cin>>confirm;
if(confirm=='n')
f2.write((char *)&emp,sizeof(emp));
else
cout<<"Record has been deleted";
}
else
f2.write((char *)&emp,sizeof(emp));
}
f1.close();
f2.close();
remove("employee.dat");
rename("temporary.dat","employee.dat");
if(fnd==0)
cout<<"No record to delete";
}
void modify()
{
fstream f1("employee.dat",ios::binary|ios::out|ios::in);
employee emp;
f1.seekg(0,ios::beg);
int eno;
cout<<"Empno to modify :";
cin>>eno;
int rec=0,fnd=0;
while(f1.read((char *)&emp,sizeof(emp)))
{
rec++;
if(eno==emp.getempno())
{
emp.putdata();
fnd=1;
cout<<"Input new data";
emp.getdata();
f1.seekg((rec-1)*sizeof(emp),ios::beg);
f1.write((char *)&emp,sizeof(emp));
cout<<"Record modified";
}
}
f1.close();
if(fnd==0)
cout<<"No record exist";
}
int main()
{
newemployee();
//display();
del();
modify();
return 0;
}