-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSequentialFileHandling.cpp
216 lines (202 loc) · 3.41 KB
/
SequentialFileHandling.cpp
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
215
216
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class student
{
char name[20];
int rollno;
public:
student()
{
name[0]='\0';
rollno=-1;
}
void getdata();
void putdata();
int returnroll()
{
return rollno;
}
};
void student::getdata()
{
cout<<"\nenter the name : " ;
cin>>name;
cout<<"\nenter roll no : ";
cin>>rollno;
}
void student::putdata()
{
cout<<"\n name is : "<<name<<endl;
cout<<"\n roll no is : "<<rollno<<endl;
}
class seq_file
{
char file_name[15];
public:
seq_file();
seq_file(char f[]);
void create();
void display();
void add();
void remove(int);
void modify(int);
};
seq_file::seq_file()
{
ofstream file("student.txt");
strcpy(file_name,"student.txt");
cout<<"default constructor";
if(file)
{
cout<<"\n file opened successfully";
file.close();
}
else
cout<<"\\n file creation error";
}
seq_file::seq_file(char f[15])
{
ofstream file;
strcpy(file_name,f);
if(file)
{
cout<<" file opened successfully";
file.close();
}
else
cout<<" file creation error";
}
void seq_file::create()
{
ofstream file;
student s;
file.open(file_name);
s.getdata();
file.write(reinterpret_cast <char *> (&s),sizeof(s));
file.close();
}
void seq_file::display()
{
ifstream file;
student s;
file.open(file_name);
//cout<<"\n rollno \t student name";
file.read(reinterpret_cast <char *> (&s),sizeof(s));
while(!file.eof())
{
s.putdata();
file.read(reinterpret_cast <char *> (&s),sizeof(s));
}
file.close();
}
void seq_file::add()
{
ofstream file;
student s;
file.open(file_name,ios::app);
s.getdata();
file.write(reinterpret_cast <char *> (&s),sizeof(s));
file.close();
}
void seq_file::remove_re(int roll)
{
ifstream file;
ofstream temp;
student s;
int flag=0;
file.open(file_name);
temp.open("temp.txt");
while(getline(file,line))
{
if(line != roll)
temp << line<< endl;
}
cout<<"the record with the roll no "<< roll<<"is deleted"<<endl;
file.close();
temp.close();
remove(file_name);
rename("temp.txt","record.txt");
/*
file.read(reinterpret_cast <char *> (&s),sizeof(s));
while(!file.eof())
if(roll==s.returnroll())
{
s.putdata();
flag=1;
}
else
{
temp.write(reinterpret_cast <char *> (&s),sizeof(s));
file.read(reinterpret_cast <char *> (&s),sizeof(s));
}
if(flag==0)
cout<<"rollno"<<roll<<"does not exists";
file.close();
temp.close();
//remove(file_name);
rename("temp.txt",file_name);*/
}
void seq_file::modify(int roll)
{
ifstream file;
ofstream temp;
student s;
int flag=0;
file.open(file_name);
temp.open("temp.txt");
file.read(reinterpret_cast <char *> (&s),sizeof(s));
while(!file.eof())
{
if(roll==s.returnroll())
{
s.putdata();
cout<<"\n enter the data to be modify : ";
s.getdata();
flag=1;
}
temp.write(reinterpret_cast <char *> (&s),sizeof(s));
file.read(reinterpret_cast <char *> (&s),sizeof(s));
}
if(flag==0)
cout<<"rollno"<<roll<<"does not exists";
file.close();
temp.close();
//remove(file_name);
rename("temp.txt",file_name);
}
int main()
{
int choice;
char f[15];
int r;
cout<<"enter file name : ";
cin>>f;
seq_file sfile(f);
do
{
cout<<"\n 1) create \n 2) display \n 3) add a record \n 4) delete a record";
cout<<"\n 5) modify a record \n\n enter your choice:";
cin>>choice;
switch(choice)
{
case 1 :sfile.create();
break;
case 2 :sfile.display();
break;
case 3 :sfile.add();
break;
case 4 :cout<<"\n enter the rollno of record to be deleted : ";
cin>>r;
sfile.remove_re(r);
break;
case 5 :cout<<"\n enter the rollno of record to be modified : ";
cin>>r;
sfile.modify(r);
break;
}
}while(choice<6);
cout<<"\n";
return 1;
}