-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential.cpp
More file actions
148 lines (143 loc) · 3.45 KB
/
Copy pathsequential.cpp
File metadata and controls
148 lines (143 loc) · 3.45 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
#include<iostream>
#include<fstream>
using namespace std;
class student
{
public:
char name[10];
int roll;
void getdata()
{
cout<<"\n Enter the roll no and name:";
cin>>roll>>name;
}
void putdata()
{
cout<<"\n The roll no and name:";
cout<<roll<<" "<<name;
}
};
class file
{
fstream fp;
public:
void create()
{
char ans;
student s;
fp.open("stu.dat",ios::out);
do
{
s.getdata();
fp.write((char *)&s,sizeof(s));
cout<<"\n More?";
cin>>ans;
}while(ans=='Y'||ans=='y');
fp.close();
}
void append()
{
char ans;
student s;
fp.open("stu.dat",ios::app);
do
{
s.getdata();
fp.write((char *)&s,sizeof(s));
cout<<"\n More?";
cin>>ans;
}while(ans=='Y'||ans=='y');
fp.close();
}
void display()
{
student s;
fp.open("stu.dat",ios::in);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof())
s.putdata();
}
fp.close();
}
void search()
{
student s; int flag=0;
int r;
cout <<"\n Enter roll to be searched:";
cin >> r;
fp.open("stu.dat",ios::in);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof() && s.roll==r)
{ flag=1; s.putdata(); break; }
}
if (flag==0) cout << "\n Not found";
fp.close();
}
void update()
{
student s; int flag=0;
int r;
cout <<"\n Enter roll to be updated:";
cin >> r;
fp.open("stu.dat",ios::in|ios::out);
while(!fp.eof())
{
fp.read((char *)&s,sizeof(s));
if (!fp.eof() && s.roll==r)
{
flag=1;
cout <<"\n Enter new data\n";
s.getdata();
fp.seekp(-1*sizeof(s),ios::cur);
fp.write((char *)&s,sizeof(s));
break;
}
}
if (flag==0) cout << "\n Not found";
fp.close();
}
void delete1()
{
student s; int flag=0;
fstream fp1;
cout <<"\n Enter roll to be deleted:";
int r; cin >> r;
fp.open("stu.dat",ios::in);
fp1.open("temp.dat",ios::out);
while(fp.read((char *)&s,sizeof(s)))
{
if ( s.roll!=r)
{
flag=1;
fp1.write((char *)&s,sizeof(s));
}
}
if (flag==0) cout << "\n Not found";
fp.close();
fp1.close();
remove("stu.dat");
rename("temp.dat","stu.dat");
}
};
int main()
{
file f; int choice;
do{cout<<"\n";
cout << "\n1.Create \n2.Display \n3.Search \n4.Append \n5.Delete \n6.Update";
cout << "\n Enter choice:"; cin >> choice;
cout<<"\n";
switch(choice)
{
case 1:f.create(); break;
case 2:f.display(); break;
case 3:f.search(); break;
case 4:f.append(); break;
case 5:f.delete1();break;
case 6:f.update();break;
}
}while(choice< 7);
}