Repository navigation
Expand file tree
/
Copy pathRecord.cpp
More file actions
80 lines (66 loc) · 2.39 KB
/
Copy pathRecord.cpp
File metadata and controls
80 lines (66 loc) · 2.39 KB
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
#include "Record.hpp"
#include <fstream>
Record::Record(int size){
numPlayers = size;
mPlayers = new Player[numPlayers];
};
Record::~Record(){
delete [] mPlayers;
}
void Record::recordsToFile(vector<Player> players){
ofstream out("record.dat", ios::binary);
if(out.good()){
for(int i = 0; i < numPlayers; i++){
//cout << "\tHELLO??" << endl;
int nameLen = players[i].getName().size();
//cout << "\t" << players[i].getName() << endl;
out.write(reinterpret_cast<char*>(&nameLen), sizeof(nameLen));
out.write(players[i].getName().c_str(), nameLen);
//cout << "\tIs there a problem?" << endl;
int* tempInt = new int;
*tempInt = players[i].getWins();
out.write(reinterpret_cast<char*>(tempInt), sizeof(int));
*tempInt = players[i].getLosses();
out.write(reinterpret_cast<char*>(tempInt), sizeof(int));
*tempInt = players[i].getPushes();
out.write(reinterpret_cast<char*>(tempInt), sizeof(int));
delete tempInt;
}
cout << "\tRECORDS TO FILE SUCCESSFUL" << endl;
out.close();
}
}
void Record::recordsFromFile(){
ifstream in("record.dat", ios::binary | ios::in);
if(in.good()){
for(int i = 0; i < numPlayers; i++){
int nameLen;
in.read(reinterpret_cast<char*>(&nameLen), sizeof(int));
char* name = new char[nameLen + 1];
in.read(name, nameLen);
name[nameLen] = '\0';
mPlayers[i].setName(string(name));
delete [] name;
int tempWins;
int tempLosses;
int tempPushes;
in.read(reinterpret_cast<char*>(&tempWins), sizeof(int));
in.read(reinterpret_cast<char*>(&tempLosses), sizeof(int));
in.read(reinterpret_cast<char*>(&tempPushes), sizeof(int));
mPlayers[i].setWins(tempWins);
mPlayers[i].setLosses(tempLosses);
mPlayers[i].setPushes(tempPushes);
}
in.close();
}
}
void Record::retrieveRecord(){
cout << "\tSCORES" << endl;
for(int i = 0; i < numPlayers; i++){
cout << mPlayers[i].getName() << "\t";
cout << mPlayers[i].getWins() << "\t";
cout << mPlayers[i].getLosses() << "\t";
cout << mPlayers[i].getPushes() << "\t";
cout << endl;
}
}