-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment-13.cpp
More file actions
210 lines (202 loc) · 3.46 KB
/
Copy pathassignment-13.cpp
File metadata and controls
210 lines (202 loc) · 3.46 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
205
206
207
208
209
210
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class stud
{
char fname[20],name[20];
long int phone;
public:
void getdata()
{
fstream f;
int choice;
cout<<"Enter file name (with extension) : ";
cin>>fname;
f.open(fname);
if(f.fail())
{
cout<<"Error opening file\nEnter choice\n";
cout<<"1. To re-open the file\n2. To create a new file with name : "<<fname<<"\n";
cin>>choice;
switch (choice)
{
case 1: getdata();
break;
case 2: create();
break;
default: cout<<"Wrong choice\n";
break;
}
}
else
{
cout<<"File opened successfully\n";
}
}
void create()
{
int num,i;
stud s;
ofstream fout;
cout<<"Enter number of students : ";
cin>>num;
fout.open(fname);
if(fout.fail())
cout<<"Failed to open file\n";
else
{
cout<<"Enter data :\n";
for(i=0;i<num;i++)
{
cout<<"Enter name : ";
cin>>s.name;
cout<<"Enter phone number : ";
cin>>s.phone;
fout.write((char*)&s,sizeof(s));
}
cout<<"File sucessfully created\n";
}
fout.close();
}
void display()
{
stud s;
ifstream fin;
fin.open(fname);
cout<<"NAME\t"<<"PHONE\n";
if(fin.fail())
cout<<"Failed to open file\n";
else
{
while(fin.read((char*)&s,sizeof(s)))
{
cout<<s.name<<"\t"<<s.phone<<endl;
}
fin.close();
}
}
void searchn()
{
char n[20];
stud s;
int flag=0;
ifstream fin;
fin.open(fname);
cout<<"Enter name to be searched : ";
cin>>n;
if(fin.fail())
cout<<"Failed to open file\n";
else
{
while(fin.read((char*)&s,sizeof(s)))
{
if(strcmp(s.name,n)==0)
{
cout<<"Entry found!!!\n";
cout<<"NAME\t"<<"PHONE\n";
cout<<s.name<<"\t"<<s.phone<<endl;
flag=1;
}
}
if(flag==0)
cout<<"No data found\n";
}
fin.close();
}
void searchp()
{
int temp;
stud s;
int flag=0;
ifstream fin;
fin.open(fname);
cout<<"Enter phone number to be searched : ";
cin>>temp;
if(fin.fail())
cout<<"Failed to open file\n";
else
{
while(fin.read((char*)&s,sizeof(s)))
{
if(s.phone==temp)
{
cout<<"Entry found!!!\n";
cout<<"NAME\t"<<"PHONE\n";
cout<<s.name<<"\t"<<s.phone<<endl;
flag=1;
}
}
if(flag==0)
cout<<"No data found\n";
}
fin.close();
}
void editpn()
{
char temp[20];
cout<<"Enter name whose ph no is to be edited:\n";
cin>>temp;
int flag=0,pos=0;
fstream f;
stud s;
f.open(fname);
if(f.fail())
{
cout<<"Failed to open the file\n";
}
else
{
while( f.read((char*)&s,sizeof(s)))
{
if(strcmp(s.name,temp)==0)
{
cout<<"RECORD FOUND........................ :-)\n";
cout<<"Enter new ph no. for "<<s.name<<"\n";
cin>>s.phone;
f.seekp(pos*sizeof(s),ios::beg);
f.write((char*)&s,sizeof(s));
flag=1;
}
else
{
pos++;
}
}
if(flag==1)
{
cout<<"RECORD EDITED SUCCESSFULLY...!!!!\n";
}
else
{
cout<<"Record not found.....\n";
}
}
f.close();
}
};
int main()
{
int choice;
stud s;
s.getdata();
do
{
cout<<"Enter choice\n1. Overwrite file\n2. display\n3. Search by name\n4. Search by phone number\n.5. Edit phone number\n6. Exit\n";
cin>>choice;
switch (choice)
{
case 1: s.create();
break;
case 2: s.display();
break;
case 3: s.searchn();
break;
case 4: s.searchp();
break;
case 5: s.editpn();
break;
}
}while(choice!=6);
return 0;
}