-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathf12.cpp
More file actions
173 lines (159 loc) · 2.79 KB
/
f12.cpp
File metadata and controls
173 lines (159 loc) · 2.79 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
//LAB PROGRAM 12. Write a C++ program to reclaim the free space resulting from the deletion of records using linked lists.
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class student
{
public:
string usn, name, sem;
void enter_data();
void display_data();
void pack();
void unpack();
}s;
struct node
{
int addr;
struct node * link;
};
typedef struct node * NODE;
NODE first = NULL;
fstream fp;
void del();
void error(int);
int main()
{
int choice,i;
system("clear");
student s;
while(1)
{
cout<<"\n1. Insert a Record\n2. Delete a record\n3.Display Records\n4. Exit\n\nEnter Choice : ";
cin>>choice;
switch(choice)
{
case 1:
s.enter_data();
s.pack();
break;
case 2:
del();
break;
case 3:
fp.open("record12.txt", ios::in);
if(!fp)
error(1);
i=0;
while(fp)
{
s.unpack();
if(s.usn.length()==0)
break;
if(s.usn[0]=='*')
continue;
cout<<"\nRecord "<< ++i;
s.display_data();
}
fp.close();
break;
default:
exit(0);
}
}
}
void student::enter_data()
{
cout<<"\nEnter Usn : ";
cin>>usn;
cout<<"\nEnter Name : ";
cin>>name;
cout<<"\nEnter Sem: ";
cin>>sem;
}
void student::display_data()
{
cout<<"\nName : "<<name<<"\nUSN : "<<usn<<"\nSem : "<<sem<<endl;
}
void student::pack()
{
string buf = usn + "|" + name + "|" + sem + "|";
if(buf.length() > 45)
{ error(2);
return;
}
while(buf.length()<46)
buf += '_';
if(first != NULL)
{
fp.open("record12.txt",ios::out|ios::in);
if(!fp)
error(1);
fp.seekp(first->addr, ios::beg);
cout<<"Reusing deleted space at position : "<<first->addr<<endl;
NODE temp = first;
first = temp->link;
delete temp;
}
else
fp.open("record12.txt",ios::out|ios::app);
fp<<buf<<endl;
fp.close();
}
void student::unpack()
{
string seg;
getline(fp,usn,'|');
getline(fp,name,'|');
getline(fp,sem,'|');
getline(fp,seg);
}
void del()
{
string usn_srch;
int pos, flag=0;
cout<<"\nEnter the USN of the student to be deleted : ";
cin>>usn_srch;
fp.open("record12.txt",ios::in);
if(!fp)
error(1);
while(fp)
{
pos = fp.tellg();
s.unpack();
if(usn_srch == s.usn)
{
flag=1;
cout<<"\nThe Following Record is Deleted at position :"<<pos<<endl;
s.display_data();
break;
}
}
if(!flag)
{
cout<<"\nRecord Not Found\n";
return;
}
fp.close();
fp.open("record12.txt",ios::out|ios::in);
if(!fp)
error(1);
fp.seekp(pos,ios::beg);
fp.put('*');
NODE temp = new struct node;
temp->addr = pos;
temp->link = first;
first = temp;
fp.close();
}
void error(int error_type)
{
switch(error_type)
{
case 1: cout<<"\nFATAL ERROR!: Unable to open the record File\n";
exit(0);
case 2: cout<<"\n ERROR!: Data exceeds record length\n";
return;
}
}