Skip to content

Commit 45788b5

Browse files
committed
Switching from fstream pointers to references.
1 parent db966ad commit 45788b5

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

include/GameBoard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class GameBoard {
1818
std::map<Coordinate, std::unique_ptr<GamePiece>> pieces;
1919
std::vector<std::unique_ptr<const Road>> roads;
2020

21-
int constructBoardFromFile(std::ifstream * file);
22-
int constructFileFromBoard(std::ofstream * file);
21+
int constructBoardFromFile(std::ifstream &file);
22+
int constructFileFromBoard(std::ofstream &file);
2323

2424
public:
2525
GameBoard();

src/GameBoard.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,52 @@ GameBoard::~GameBoard() {
1010

1111

1212
int GameBoard::save_Board(std::string filename){
13-
std::ofstream * file = new std::ofstream;
14-
file->exceptions(std::ofstream::failbit | std::ofstream::badbit);
13+
std::ofstream file;
14+
file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
1515
try {
1616
filename = filename + ".wocs";
17-
file->open(filename.c_str());
17+
file.open(filename.c_str());
1818
constructFileFromBoard(file);
19-
file->close();
20-
delete(file);
19+
file.close();
2120
return 0;
2221
} catch (std::ofstream::failure e) {
2322
std::cerr << "Exception opening/closing/writing file: " << e.what();
24-
delete (file);
2523
}
2624
return -1;
2725

2826
}
2927

3028
int GameBoard::load_Board(std::string filename){
31-
std::ifstream * file = new std::ifstream;
29+
std::ifstream file;
3230
try {
3331
filename = filename + ".wocs";
34-
file->open(filename.c_str());
32+
file.open(filename.c_str());
3533
constructBoardFromFile(file);
36-
file->close();
37-
delete(file);
34+
file.close();
3835
return 0;
3936
} catch (std::ifstream::failure e) {
4037
std::cerr << "Exception opening/closing/reading file: " << e.what();
41-
delete (file);
4238
}
4339
return -1;
4440
}
4541

46-
int GameBoard::constructBoardFromFile(std::ifstream * file){
42+
int GameBoard::constructBoardFromFile(std::ifstream &file){
4743
//Parse and construct the board from the file
4844
//@ TODO
4945
std::string line;
50-
if (file->is_open()) {
51-
while (getline(*file, line)) {
46+
if (file.is_open()) {
47+
while (getline(file, line)) {
5248
std::cout << line << '\n';
5349
}
5450
}
5551
return 0;
5652
}
5753

5854

59-
int GameBoard::constructFileFromBoard(std::ofstream * file){
55+
int GameBoard::constructFileFromBoard(std::ofstream &file){
6056
//Construct the file based on the structure of the board
6157
//@ TODO
62-
*file << "Hello World!";
58+
file << "Hello World!";
6359
return 0;
6460
}
6561

0 commit comments

Comments
 (0)