-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cpp
193 lines (171 loc) · 3.64 KB
/
List.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
#include "List.h"
#include<conio.h>
#include<dos.h>
void List::AddTask()
{
fstream logfile("logfile.dat", ios::out | ios::binary | ios::app);
if (!logfile)
{
cout << "ERROR! in opening file\n";
return;
}
Tasks task; //object of task class;
task.AddTask();
if (checkSamekey(task.GetId()))
{
cout << "Task exist with same id\n\a";
cout << "Please change id\n";
logfile.close();
return;
}
logfile.write(reinterpret_cast<char*>(&task), sizeof(task));
cout << "Task Added\n";
logfile.close();
}
void List::ModifyTask(int Id)
{
fstream logfile("logfile.dat", ios::out | ios::in | ios::binary);
if (!logfile)
{
cout << "ERROR! in opening file\n";
return;
}
Tasks task; //object of task class;
bool flag = true;//to check if task is present
while (logfile.read(reinterpret_cast<char*>(&task), sizeof(task)) && flag)
{
if (task.GetId() == Id)
{
task.ModifyTask();
int pos = -1 * static_cast<int>(sizeof(task));
logfile.seekp(pos, ios::cur);
logfile.write(reinterpret_cast<char*>(&task), sizeof(task));
flag = false;
}
}
logfile.close();
if (flag)
{
cout << "NO task with this ID\n";
}
else
{
cout << "Task Modified\n";
}
}
void List::DeleteTask(int Id)
{
ifstream logfile;
ofstream TempFile;
TempFile.open("Temp.dat", ios::binary);
logfile.open("logfile.dat", ios::binary);
if (!logfile || !TempFile)
{
cout << "Error! File not open " << endl;
return;
}
Tasks task; //object of task class;
while (logfile.read(reinterpret_cast<char*>(&task), sizeof(task)))
{
if (task.GetId() != Id)
{
TempFile.write(reinterpret_cast<char*>(&task), sizeof(task));
}
}
logfile.close();
TempFile.close();
remove("logfile.dat");
rename("Temp.dat", "logfile.dat");
cout << "\nThis record has been Deleted\n";
}
void List::ShowTasks()
{
fstream logfile("logfile.dat", ios::out | ios::in | ios::binary);
if (!logfile)
{
cout << "ERROR! in opening file\n";
return;
}
int count = 1;
Tasks task; //object of task class;
cout << "======================================================================\n";
cout << "#no Id Task Status\n";
cout << "======================================================================\n";
while (logfile.read(reinterpret_cast<char*>(&task), sizeof(task)))
{
task.ShowTask(count);
count++;
}
logfile.close();
}
void List::RemoveAll()
{
cout << "Are You Sure?? Y/N: ";
char choice;
choice = getchar();
cin >> choice;
if (choice == 'n' || choice == 'N')
{
return;
}
if (remove("logfile.dat"))
{
cout << "data has been removed\a\n";
}
else
{
cout << "error in removing\n";
}
}
void List::SetStatus(int Id)
{
fstream logfile("logfile.dat", ios::out | ios::in | ios::binary);
if (!logfile)
{
cout << "ERROR! in opening file\n";
return;
}
Tasks task; //object of task class;
bool flag = true;//to check if task is present
while (logfile.read(reinterpret_cast<char*>(&task), sizeof(task)) && flag)
{
if (task.GetId() == Id)
{
task.SetStatus();
int pos = -1 * static_cast<int>(sizeof(task));
logfile.seekp(pos, ios::cur);
logfile.write(reinterpret_cast<char*>(&task), sizeof(task));
flag = false;
}
}
logfile.close();
if (flag)
{
cout << "NO task with this ID\n";
}
else
{
cout << "Status Set\n";
}
}
bool List::checkSamekey(int ID)
{
bool flag = false;
Tasks obj;
fstream Datafile;
Datafile.open("logfile.dat", ios::in | ios::binary);
if (!Datafile)
{
cout << "Error! File not open " << endl;
return 0;
}
while (Datafile.read(reinterpret_cast<char*>(&obj), sizeof(obj)) && !flag)
{
if (obj.GetId() == ID)
{
flag = true;
}
}
Datafile.close();
return flag;
}