-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFile.cpp
More file actions
214 lines (213 loc) · 6.14 KB
/
File.cpp
File metadata and controls
214 lines (213 loc) · 6.14 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
211
212
213
214
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string.h>
//#include<conio.h>
#include<stdlib.h>
using namespace std;
class STUD_CLASS
{
typedef struct student
{
char name[10];
int rollno;
char div;
char Address[20];
}Rec;
Rec Records;
public:
void Create(char f[]);
void Display(char f[]);
void Update(char f[]);
void Delete(char f[]);
void Append(char f[]);
int Search(char f[]);
};
void STUD_CLASS::Create(char f[])
{
char ch='y';
fstream seqfile;
seqfile.open(f,ios::in|ios::out|ios::binary);
do
{
cout<<"\n Enter Name: ";
cin>>Records.name;
cout<<"\n Enter roll no: ";
cin>>Records.rollno;
cout<<"\n Enter div: ";
cin>>Records.div;
cout<<"\n Enter Address ";
cin>>Records.Address;
//then write the record containing this data in the file
seqfile.write((char*)&Records,sizeof(Records));
cout<<"\nDo you want to add more records?";
cin>>ch;
}while(ch=='y');
seqfile.close();
}
void STUD_CLASS::Display(char f[])
{
fstream seqfile;
int n,m,i;
seqfile.open(f,ios::in|ios::out|ios::binary);
//positioning the pointer in the file at the beginning
seqfile.seekg(0,ios::beg);
cout<<"\n The Contents of file are ..."<<endl;
//read the records sequentially
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(Records.rollno!=-1)
{
cout<<"\nName: "<<Records.name;
cout<<"\nRollNO: "<<Records.rollno;
cout<<"\nDivision: "<<Records.div;
cout<<"\nAddress: "<<Records.Address;
cout<<"\n";
}
}
//int last_rec=seqfile.tellg();//last record position
seqfile.close();
}
void STUD_CLASS::Update(char f[])
{
int pos;
cout<<"\n For updation,";
fstream seqfile;
seqfile.open(f,ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
//obtaining the position of desired record in the file
pos=Search(f);
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
//calculate the actual offset of the desired record in the file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for modification
cout<<"\n Enter the values for updation...";
cout<<"\n Name: ";cin>>Records.name;
cout<<"\n Rollno: ";cin>>Records.rollno;
cout<<"\n Div: ";cin>>Records.div;
cout<<"\n Address: ";cin>>Records.Address;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is updated!!!";
}
void STUD_CLASS::Delete(char f[])
{
int id,pos;
cout<<"\n For deletion,";
fstream seqfile;
seqfile.open(f,ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);//seeking for reading purpose
pos=Search(f);//finding pos. for the record to be deleted
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
}
//calculate offset to locate the desired record in the file
int offset=pos*sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for deletion
strcpy(Records.name,"");
Records.rollno=-1;
Records.div=' ';
strcpy(Records.Address,"");
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is Deleted!!!";
}
void STUD_CLASS::Append(char f[])
{
fstream seqfile;
seqfile.open(f,ios::ate|ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
i++;//going through all the records
// for reaching at the end of the file
}
//instead of above while loop
//we can also use seqfile.seekg(0,ios::end)
//for reaching at the end of the file
seqfile.clear();//turning off EOF flag
cout<<"\n Enter the record for appending";
cout<<"\nName: ";cin>>Records.name;
cout<<"\nRoll NO ";cin>>Records.rollno;
cout<<"\nDiv: ";cin>>Records.div;
cout<<"\nAddress: ";cin>>Records.Address;
seqfile.write((char*)&Records,sizeof(Records));
seqfile.seekg(0);//reposition to start(optional)
seqfile.close();
cout<<"\n The record is Appended!!!";
}
int STUD_CLASS::Search(char f[])
{
fstream seqfile;
int id,pos;
cout<<"\n Enter the Roll no for searching the record ";
cin>>id;
seqfile.open(f,ios::ate|ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
pos=-1;
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(id==Records.rollno)
{
pos=i;
break;
}
i++;
}
return pos;
}
int main()
{
STUD_CLASS List;
char ans='y';
int choice,key;
char filename[20];
cout<<endl<<"please enter filename(make sure file is created in same folder)";
cin>>filename;
//clrscr();
do
{
cout<<"\n Main Menu "<<endl;
cout<<"\n 1.Create";
cout<<"\n 2.Display";
cout<<"\n 3.Update";
cout<<"\n 4.Delete";
cout<<"\n 5.Append";
cout<<"\n 6.Search";
cout<<"\n 7.Exit";
cout<<"\n Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:List.Create(filename);
break;
case 2:List.Display(filename);
break;
case 3:List.Update(filename);
break;
case 4:List.Delete(filename);
break;
case 5:List.Append(filename);
break;
case 6:key=List.Search(filename);
if(key<0)
cout<<"\n Record is not present in the file";
else
cout<<"\n Record is present in the file";
break;
case 7:exit(0);
}
cout<<"\n\t Do you want to go back to Main Menu?";
cin>>ans;
}while(ans=='y');
}