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
|
#include "Exercise5.h"
#include <fstream>
#include <iostream>
#include <cstring>
/* Private */
void TransportTicket::free() {
delete[] source;
delete[] destination;
}
void TransportTicket::copyFrom(const TransportTicket& other) {
this->id = other.id;
this->source = new char[strlen(other.source)+1];
strcpy(this->source, other.source);
this->destination = new char[strlen(other.destination)+1];
strcpy(this->destination, other.destination);
this->price = other.price;
}
/* Public */
TransportTicket::TransportTicket(unsigned id, const char* source, const char* destination, float price) {
this->id = id;
this->source = new char[strlen(source)+1];
strcpy(this->source, source);
this->destination = new char[strlen(destination)+1];
strcpy(this->destination, destination);
this->price = price;
}
void TransportTicket::Print() {
std::cout << id << " " << source << " " << destination << " " << price << std::endl;
}
TransportTicket::TransportTicket() {
id = price = 0;
source = destination = nullptr;
}
TransportTicket::~TransportTicket() {
free();
}
TransportTicket::TransportTicket(const TransportTicket& other) {
copyFrom(other);
}
TransportTicket& TransportTicket::operator=(const TransportTicket& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
TransportTicket::TransportTicket(TransportTicket&& other) {
this->id = other.id;
this->source = other.source;
this->destination = other.destination;
this->price = other.price;
other.source = nullptr;
other.destination = nullptr;
}
TransportTicket& TransportTicket::operator=(TransportTicket&& other) {
if (this != &other) {
free();
this->id = other.id;
this->source = other.source;
this->destination = other.destination;
this->price = other.price;
other.destination = nullptr;
other.source = nullptr;
}
return *this;
}
void TransportTicket::SaveText(const char* fileName) {
std::ofstream outFile(fileName);
if (!outFile.is_open()) {
return;
}
outFile << id << ',' << source << ',' << destination << ',' << price;
outFile.close();
}
unsigned distanceUntilComma(std::ifstream& inFile) {
unsigned start = inFile.tellg();
while (inFile.peek() != ',') {
inFile.get();
}
unsigned len = (unsigned)inFile.tellg() - start + 1;
inFile.seekg(start, std::ios::beg);
return len;
}
void TransportTicket::LoadText(const char* fileName) {
std::ifstream inFile(fileName);
if (!inFile.is_open()) {
return;
}
free();
inFile >> id;
inFile.ignore();
unsigned sourceLen = distanceUntilComma(inFile);
source = new char[sourceLen+1];
inFile.get(source, sourceLen);
source[sourceLen] = '\0';
inFile.ignore();
unsigned destinationLen = distanceUntilComma(inFile);
destination = new char[destinationLen+1];
inFile.get(destination, destinationLen);
destination[destinationLen] = '\0';
inFile.ignore();
inFile >> price;
inFile.close();
}
void TransportTicket::SaveBinary(const char* fileName) {
std::ofstream outFile(fileName, std::ios::binary);
if (!outFile.is_open()) {
return;
}
outFile.write((const char*)&id, sizeof(id));
outFile.write((const char*)&price, sizeof(price));
unsigned sourceLen = strlen(source)+1;
outFile.write((const char*)&sourceLen, sizeof(sourceLen));
outFile.write(source, sizeof(char) * sourceLen);
unsigned destinationLen = strlen(destination)+1;
outFile.write((const char*)&destinationLen, sizeof(destinationLen));
outFile.write(destination, sizeof(char) * destinationLen);
outFile.close();
}
void TransportTicket::LoadBinary(const char* fileName) {
std::ifstream inFile(fileName, std::ios::binary);
if (!inFile.is_open()) {
return;
}
free();
inFile.read((char*)&id, sizeof(id));
inFile.read((char*)&price, sizeof(price));
unsigned sourceLen = 0;
inFile.read((char*)&sourceLen, sizeof(sourceLen));
source = new char[sourceLen+1];
inFile.read(source, sizeof(char) * sourceLen);
unsigned destinationLen = 0;
inFile.read((char*)&destinationLen, sizeof(destinationLen));
destination = new char[destinationLen+1];
inFile.read(destination, sizeof(char) * destinationLen);
inFile.close();
}
int main() {
TransportTicket t1(193, "Sofia", "Bourgas", 15.50);
t1.SaveText("t1.txt");
TransportTicket t2(782, "Sofia", "Plovdiv", 8.99);
t2.SaveBinary("t2.dat");
TransportTicket t3;
t3.LoadText("t1.txt");
t3.Print();
t3.LoadBinary("t2.dat");
t3.Print();
}
|