-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathtextToBinary.cpp
115 lines (86 loc) · 3.46 KB
/
textToBinary.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
//*****************************************************************************************************
// Text File to Binary Conversion
//
// This program reads tester information from a text file and writes the information to a binary
// file.
//
// Other files required:
// 1. Testers.txt - number of testers and the tester information
//
//*****************************************************************************************************
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//*****************************************************************************************************
const int NAME_SIZE = 20;
struct Date {
int month;
int day;
int year;
};
struct Person {
char name[NAME_SIZE];
double score;
Date testTaken;
};
Person *readTesters(const string &fileName, int &num);
void writeTesters(const string &fileName, Person people[], int numP);
//*****************************************************************************************************
int main() {
int numP = 0;
string fileName;
Person *people = nullptr;
people = readTesters("Testers.txt", numP); // read data from text file
writeTesters("Testers.dat", people, numP); // write data to binary file
delete[] people;
people = nullptr;
return 0;
}
//*****************************************************************************************************
Person *readTesters(const string &fileName, int &num) {
Person *p = nullptr;
ifstream f(fileName);
if (f.is_open()) {
f >> num;
f.ignore();
p = new Person[num];
for (int i = 0; i < num; ++i) {
f.getline(p[i].name, NAME_SIZE);
f >> p[i].score;
f.ignore();
f >> p[i].testTaken.month;
f.ignore();
f >> p[i].testTaken.day;
f.ignore();
f >> p[i].testTaken.year;
f.ignore();
}
f.close();
} else {
cerr << "Error: Unable to open file\n";
}
return p;
}
//*****************************************************************************************************
void writeTesters(const string &fileName, Person people[], int numP) {
ofstream f(fileName, ios::binary); // open file for writing in binary mode
f.write(reinterpret_cast<char *>(&numP), sizeof(int));
f.write(reinterpret_cast<char *>(people), sizeof(Person) * numP);
f.close();
}
//*****************************************************************************************************
/*
reinterpret_cast<new_type>(expression)
C++ type cast operator used to cast an expression to a different type, regardless of the relationship
between the original type and the new type.
*****************************************************************************************************
f.write(reinterpret_cast<char*>(&numP), sizeof(int));
(&numP) - casts it to a char* pointer, allowing it to be written to the binary file
sizeof(int) - specifies the size of the data being written is an int type
*****************************************************************************************************
f.write(reinterpret_cast<char*>(people), sizeof(Person) * numP);
(people) - casts it to a char* pointer, allowing it to be written to the binary file.
sizeof(Person) * numP - specifies the size of the data being written is an array of Person structs with
numP number of elements.
*/