Skip to content
Snippets Groups Projects
Commit b687835e authored by aurora's avatar aurora
Browse files

Write to/read from file

parent 3b621cb6
Branches
No related tags found
No related merge requests found
......@@ -2,6 +2,8 @@
#include <iostream> // cout, cin
#include <string> // string
#include <vector> // vector
#include <iomanip> // setw
#include <fstream> // ifstream, ofstream
#include "LesData2.h"
using namespace std;
......@@ -25,6 +27,8 @@ public:
void writePlayer() const;
void updatePlayer();
int playerWin() {wins = 0;};
void writeToFilePlayer(ofstream & out);
void readFromFilePlayer(ifstream & in);
};
// vectorer:
......@@ -195,6 +199,32 @@ int Player::playerWin() {
}
/**
*
* @param in - the file being read from
*/
void Player::readFromFilePlayer(ifstream & in){
getline(in,name);
in >> rating; in.ignore();
in >> playerID; in.ignore();
getline(in, club);
}
/**
*
* @param out - the file being written to
*/
void Player::writeToFilePlayer(ofstream & out){
out << setw(STRLEN) << name
<< setw(STRLEN) << rating
<< setw(STRLEN) << playerID
<< setw(STRLEN) << club
<<"\n";
}
void results() {
}
......@@ -230,10 +260,41 @@ cout << "\nMenu: \n"
}
/**
* Writes all data belonging to the Player class to the file
*/
void writeToFile(){
ofstream outfile("PlayerList.dta");
if(outfile){
for(int i = 0; i<gPlayers.size(); i++)
gPlayers[i]->writeToFilePlayer(outfile);
outfile.close();
}
else
cout << "\nCouldn't find PlayerList.dta\n"; //maybe unneccesary, considering it's also checked when reading data from file
}
/**
* Reads all data belonging to the Player class from the file
*/
void readFromFile(){
ifstream infile("PlayerList.dta");
if(infile){
while(!infile.eof()){
newPlayer = new Player();
newPlayer ->readFromFilePlayer(infile);
gPlayers.push_back(newPlayer);
}
infile.close();
}
else
cout<<"\nCouldn't find PlayerList.dta!\n";
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment