-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathioReadWrite.cpp
102 lines (88 loc) · 2.91 KB
/
ioReadWrite.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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
string fileName = "dataFile";
struct SourceData{
int zoneID;
double temp[3];
double power[3];
};
void WriteSourceDataToFile(vector<SourceData>& sources)
{
ofstream outFile(fileName, ios::binary);
if (!outFile)
{
return;
}
for (auto& source : sources)
{
outFile.write(reinterpret_cast<const char*>(&source), sizeof(source));
}
outFile.close();
}
void ReadSourceDataFromFile(vector<SourceData>& sources)
{
ifstream inFile(fileName, ios::binary);
if (!inFile)
{
return;
}
SourceData srcData;
while(inFile.read(reinterpret_cast<char*>(&srcData), sizeof(srcData)))
{
sources.emplace_back(srcData);
}
inFile.close();
}
void ReadSourceDataByIndex(int index, SourceData& srcData)
{
ifstream inFile(fileName, ios::binary);
if (!inFile)
{
return;
}
inFile.seekg(index * sizeof(SourceData), std::ios::beg);
inFile.read(reinterpret_cast<char*>(&srcData), sizeof(srcData));
inFile.close();
}
double InterpolatePowerForTemperature(int temperature, SourceData& srcData)
{
size_t ii = 0;
for (; ii < 3; ++ii)
{
if (ii < 2 && (srcData.temp[ii] < temperature && srcData.temp[ii + 1] > temperature))
{
break;
}
}
double x1 = srcData.temp[ii];
double x2 = srcData.temp[ii + 1];
double y1 = srcData.power[ii];
double y2 = srcData.power[ii + 1];
return y1 + (y2 - y1) * (temperature - x1) / (x2 - x1);
}
int main() {
SourceData srcData1{20, {20,25,30}, {0.2, 0.3, 0.5}};
SourceData srcData2{30, {20,30,35}, {0.25, 0.4, 0.7}};
SourceData srcData3{40, {20,45,50}, {0.22, 0.5, 0.8}};
//Write source data to a file in binary format
vector<SourceData> writeSources{srcData1, srcData2, srcData3};
WriteSourceDataToFile(writeSources);
//Read source data to from a file in binary format
vector<SourceData> readSources;
ReadSourceDataFromFile(readSources);
for (auto& source : readSources)
{
cout << "Zone ID : " << source.zoneID << endl
<< "Temperatures: " << source.temp[0] << " " << source.temp[1] << " " << source.temp[2] << endl
<< "Powers: " << source.power[0] << " " << source.power[1] << " " << source.power[2] << endl;
}
//Read only a particular source data from a file using an index
SourceData readSrcData_2;
ReadSourceDataByIndex(1, readSrcData_2);
cout << "Zone ID : " << readSrcData_2.zoneID << endl
<< "Temperatures: " << readSrcData_2.temp[0] << " " << readSrcData_2.temp[1] << " " << readSrcData_2.temp[2] << endl
<< "Powers: " << readSrcData_2.power[0] << " " << readSrcData_2.power[1] << " " << readSrcData_2.power[2] << endl;
cout << "Interpolate power value: " << InterpolatePowerForTemperature(32, readSrcData_2) << endl;
};