-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveToFile.cpp
More file actions
55 lines (53 loc) · 1.88 KB
/
saveToFile.cpp
File metadata and controls
55 lines (53 loc) · 1.88 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
/*
* File: saveToFile.cpp
* Author: James Fregeau
* Created on December 11th, 2025, 4:21 PM
* To save the games to a file for trackability
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#include "saveToFile.h"
//Saves the Player, and dealer to file. Also stores the round that was played and how much the player won/ lost
void SaveToFile :: save(const string &file, Player *player, Round round){
vector<string> data;
//Player exists boolean
bool found =false;
string line, srch;
ifstream f(file);
//Player to serach if exists
srch = "Player: "+player->getName();
//Need to store the file in a vector for if we need to append the round to an existing player
while(getline(f, line)){
//Overwrites the data of Round if the player already exists
if(line == srch){
found = true;
data.push_back(line);
getline(f,line);
//Rounds payout down to nearest int. Shouldnt be an issue because you can only get a decimal as payout if blackjack is hit and it is such a small amount of money
line += ", {Dealer: "+ round.getDealerName() + "| Payout: " + to_string(static_cast<int>(round.getPayOut()))+"} ";
data.push_back(line);
}else{
data.push_back(line);
}
}
f.clear();
f.close();
//if player exists
if(found){
//Wipes file and rewrites it with the new data appended to that player
ofstream f(file, ios::trunc);
for(int i=0; i<data.size();i++){
f <<data[i]<<"\n";
}
//If player doesnt exist
}else{
//Opens the file and just appends it to the end of the file
ofstream f(file, ios::app);
f <<"\n"<<srch<<"\n";
f << "Rounds: {Dealer: "<< round.getDealerName() << ", Payout: " << round.getPayOut()<<"} ";
}
f.close();
}