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
|
#include "Exercise3.h"
#include <fstream>
#include <iostream>
/* Public */
Thermometer::Thermometer(unsigned maxMeasurable, unsigned minMeasurable, unsigned currentTemperature) {
this->maxMeasurable = maxMeasurable;
this->minMeasurable = minMeasurable;
this->currentTemperature = currentTemperature;
}
void Thermometer::Print() {
std::cout << maxMeasurable << " " << minMeasurable << " " << currentTemperature << std::endl;
}
void Thermometer::SaveText(const char* fileName) {
std::ofstream outFile(fileName);
if (!outFile.is_open()) {
return;
}
outFile << maxMeasurable << "," << minMeasurable << "," << currentTemperature;
outFile.close();
}
void Thermometer::LoadText(const char* fileName) {
std::ifstream inFile(fileName);
if (!inFile.is_open()) {
return;
}
inFile >> maxMeasurable;
inFile.ignore();
inFile >> minMeasurable;
inFile.ignore();
inFile >> currentTemperature;
inFile.close();
}
void Thermometer::SaveBinary(const char* fileName) {
std::ofstream outFile(fileName, std::ios::binary);
if (!outFile.is_open()) {
return;
}
outFile.write((const char*)&maxMeasurable, sizeof(maxMeasurable));
outFile.write((const char*)&minMeasurable, sizeof(minMeasurable));
outFile.write((const char*)¤tTemperature, sizeof(currentTemperature));
outFile.close();
}
void Thermometer::LoadBinary(const char* fileName) {
std::ifstream inFile(fileName, std::ios::binary);
if (!inFile.is_open()) {
return;
}
inFile.read((char*)&maxMeasurable, sizeof(maxMeasurable));
inFile.read((char*)&minMeasurable, sizeof(minMeasurable));
inFile.read((char*)¤tTemperature, sizeof(currentTemperature));
inFile.close();
}
int main() {
Thermometer t1(100, 0, 20);
t1.SaveText("t1.txt");
Thermometer t2(50, 20, 36);
t2.SaveBinary("t2.dat");
Thermometer t3(5, 5, 5);
t3.LoadText("t1.txt");
t3.Print();
t3.LoadBinary("t2.dat");
t3.Print();
}
|