This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteToFile.h
118 lines (105 loc) · 3.25 KB
/
WriteToFile.h
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
#pragma once
#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
using std::string;
using std::ofstream;
using std::istream;
using std::ifstream;
using std::ios;
class WriteToFile {
public:
static void writeRecordsToBinaryFile(Record* records, int numberOfRecords, ofstream& file) {
for (int i = 0; i < numberOfRecords; i++) {
string* tempValues = new string[records[i].getNoValues()];
for (int j = 0; j < records[i].getNoValues(); j++) {
file.write(tempValues[j].c_str(), sizeof(string));
}
delete[] tempValues;
file.write((char*)records[i].getNoValues(), sizeof(int));
}
}
static void writeFieldsToBinaryFile(Field* fields, int numberOfFields, ofstream& file) {
for (int i = 0; i < numberOfFields; i++) {
file.write(fields[i].getName().c_str(), sizeof(string));
file.write(fields[i].getType().c_str(), sizeof(string));
file.write(fields[i].getDefaultValue().c_str(), sizeof(string));
}
}
static void writeTablesToBinaryFile(Table* tables, int numberOfTables, ifstream& binaryTables) {
ofstream tableStructure("tables.bin", ios::out | ios::binary);
for (int i = 0; i < numberOfTables; i++) {
tableStructure.write((char*)&tables[i], sizeof(tables[i]));
}
tableStructure.close();
remove("binaryTables.bin");
rename("tables.bin", "binaryTables.bin");
}
static void writeRecordsToBinaryFile(Record* records, int numberOfRecords, ifstream& binaryRecords) {
ofstream recordStructure("records.bin", ios::out | ios::binary);
for (int i = 0; i < numberOfRecords; i++) {
recordStructure.write((char*)&records[i], sizeof(records[i]));
}
recordStructure.close();
remove("binaryRecords.bin");
rename("records.bin", "binaryRecords.bin");
}
static void allocSpaceTable(Table*& tables, int& numberOfTables, int spaces) {
if (tables != nullptr) {
Table* tempTables = new Table[numberOfTables + spaces];
for (int i = 0; i < numberOfTables; i++) {
tempTables[i] = tables[i];
}
delete[] tables;
numberOfTables += spaces;
tables = new Table[numberOfTables];
for (int i = 0; i < numberOfTables; i++) {
tables[i] = tempTables[i];
}
delete tempTables;
}
else {
tables = new Table[spaces];
numberOfTables += spaces;
}
}
static void allocSpaceField(Field*& fields, int& numberOfFields, int spaces) {
if (fields != nullptr) {
Field* tempFields = new Field[numberOfFields + spaces];
for (int i = 0; i < numberOfFields; i++) {
tempFields[i] = fields[i];
}
delete[] fields;
numberOfFields += spaces;
fields = new Field[numberOfFields];
for (int i = 0; i < numberOfFields; i++) {
fields[i] = tempFields[i];
}
delete[] tempFields;
}
else {
fields = new Field[spaces];
numberOfFields += spaces;
}
}
static void allocSpaceRecord(Record*& records, int& numberOfRecords, int spaces) {
if (records != nullptr) {
Record* tempRecords = new Record[numberOfRecords + spaces];
for (int i = 0; i < numberOfRecords; i++) {
tempRecords[i] = records[i];
}
delete[] records;
numberOfRecords += spaces;
records = new Record[numberOfRecords];
for (int i = 0; i < numberOfRecords; i++) {
records[i] = tempRecords[i];
}
delete[] tempRecords;
}
else {
records = new Record[spaces];
numberOfRecords += spaces;
}
}
};