Repository navigation
Expand file tree
/
Copy pathFile handling.cpp
More file actions
153 lines (121 loc) · 2.23 KB
/
Copy pathFile handling.cpp
File metadata and controls
153 lines (121 loc) · 2.23 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
#include<iostream>
#include<fstream>
using namespace std;
class student{
public:
int roll;
char div;
char name[10];
void take()
{
cout<<"\nEnter name:";
cin>>name;
cout<<"\nEnter roll no:";
cin>>roll;
cout<<"\nEnter div:";
cin>>div;
}
void put()
{
cout<<"Name:"<<name<<endl;
cout<<"Roll no:"<<roll<<endl;
cout<<"Div:"<<div<<endl;
}
};
int main()
{
int ch;
int n;
do{
cout<<"1.create and write\n2.read\n3.search\n4.append\n5.delete\n";
cout<<"Choose option:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter no of students:";
cin>>n;
student s[10];
int i;
fstream fp;
fp.open("student.txt",ios::out);
cout<<endl;
for(i=0;i<n;i++)
{
s[i].take();
fp.write((char*)&s[i],sizeof(s[i]));
}
break;
case 2:
fstream fr;
fr.open("student.txt",ios::in);
student s1;
while(fr.read((char*)&s1,sizeof(s1)))
{
s1.put();
}
break;
case 3:
student s;
int r;
int flag=0;
cout<<"Enter roll no to be searched:";
cin>>r;
fstream fs;
fs.open("student.txt",ios::in);
while(fs.read((char*)&s,sizeof(s)))
{
if(s.roll==r)
{
cout<<"record found\n";
s.put();
flag=1;
}
}
if(flag==0)
cout<<"Record not found\n";
break;
case 4:
student s[10];
int i;
int n1;
fstream fa;
fa.open("student.txt",ios::app);
cout<<"Enter no of student you want to add:";
cin>>n1;
for(i=0;i<n1;i++)
{
s[i].take();
fa.write((char*)&s[i],sizeof(s[i]));
}
break;
case 5:
fstream fp;
fstream fp1;
int delroll;
int flag=0;
student s;
fp.open("student.txt",ios::in);
fp1.open("stud.txt",ios::out);
cout<<"Enter roll to be deleted:";
cin>>delroll;
while(fp.read((char*)&s,sizeof(s)))
{
if(s.roll==delroll)
{
cout<<"record found\n";
s.put();
flag=1;
}
else
fp1.write((char*)&s,sizeof(s));
}
remove("student.txt");
rename("stud.txt","student.txt");
if(flag==0)
cout<<"Record not found\n";
break;
}
}while(ch!=0);
return 0;
}