diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp index dad8d2700c55e1f35884564a0b11e06ff598c0f3..ed098c03d75b38d760651a09589fb48ab375fe9a 100644 --- a/src/kode/Tournament.cpp +++ b/src/kode/Tournament.cpp @@ -190,17 +190,7 @@ void Tournament::endTournament() */ void Tournament::writeToFileTournament(ofstream &out) { - ofstream outfile("tournamentData.dta"); - if (outfile) - { - for (int i = 0; i < gTournaments.size(); i++) - gTournaments[i]->writeToFileTournament(outfile); - - outfile.close(); - } - else - cout << "\nCouldn't find tournamentData.dta\n"; // maybe unneccesary, considering it's also checked when reading data from file } /** @@ -208,23 +198,5 @@ void Tournament::writeToFileTournament(ofstream &out) */ void Tournament::readFromFileTournaments(ifstream &in) { - Tournament *tournamentNew; - - ifstream infile("tournamentData.dta"); - - if (infile) - { - while (!infile.eof()) - { - tournamentNew = new Tournament(); - //tournamentNew->readFromFileTournaments(infile); - gTournaments.push_back(tournamentNew); - } - - infile.close(); - } else { - cout<<"\nCouldn't find tournamentData.dta!\n"; - } - infile.close(); } \ No newline at end of file diff --git a/src/kode/checkmate.cpp b/src/kode/checkmate.cpp index 1583b504e8e561d84755c2c84d492c26db049c34..045802bf99442e8cf790da2b1485f16e7660d962 100644 --- a/src/kode/checkmate.cpp +++ b/src/kode/checkmate.cpp @@ -3,6 +3,7 @@ #include <vector> // vector #include <iomanip> // setw #include <fstream> // ifstream, ofstream +#include "writeToFile.cpp" #include "LesData3.h" #include "player.h" #include "Tournament.h" @@ -27,8 +28,8 @@ int main() { char command; - //readFromFile(); - //readFromFileTournament(); + readFromFile(); + readFromFileTournament(); do { @@ -90,8 +91,8 @@ int main() }while (command != 'Q'); - // writeToFileTournament(); - // writeToFile(); + writeToFileTournament(); + writeToFile(); return 0; } diff --git a/src/kode/functions.h b/src/kode/functions.h index e319466b6f58519cf5b0b382b238c52227b0f84f..3c71103317665118ac2f05eaea316aa2963b2340 100644 --- a/src/kode/functions.h +++ b/src/kode/functions.h @@ -16,5 +16,9 @@ void viewPlayers(); //+ void editPlayer(); //+ void startNewTournament();//+ void deletePlayer();//- +void readFromFile(); +void readFromFileTournament(); +void writeToFile(); +void WriteToFileTournament(); #endif //CHECKMATE_FUNCTIONS_H diff --git a/src/kode/readFromFile.cpp b/src/kode/readFromFile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..abb802f6c5869bfaa3829beca8fccff7d78802e7 --- /dev/null +++ b/src/kode/readFromFile.cpp @@ -0,0 +1,62 @@ + +#include <iostream> // cout, cin +#include <string> // string +#include <vector> // vector +#include <iomanip> // setw +#include <fstream> // ifstream, ofstream +#include "LesData3.h" +#include "player.h" +#include "Tournament.h" +#include "functions.h" + +using namespace std; + +extern vector<Tournament *> gTournaments; +extern vector<Player*> playerList; +extern vector<Player*> sortedRankings; + +void readFromFile() { + + ifstream inFile("PlayerList.dta"); // File object to read from + + Player* newPlayer; // Helping pointer to new object + + + if(inFile) { + cout << "\n\n\t\t.... READING FROM FILE: PLAYERLIST.DTA ....\n\n\n"; + + while(!inFile.eof()) { // while NOT end of file do this + newPlayer = new Player; + newPlayer->readFromFilePlayer(inFile); + playerList.push_back(newPlayer); + } + inFile.close(); + } else { + cout << "\n\n\t\t.... CAN NOT FIND FILE: PLAYERLIST.DTA ....\n\n\n"; + } + +} + +void readFromFileTournament() { + + Tournament *tournamentNew; // Helping pointer to new object + + ifstream infile("tournamentData.dta"); // File object to read from + + if (infile) + { + cout << "\n\n\t\t.... READING FROM FILE: TOURNAMENTDATA.DTA ....\n\n\n"; + while (!infile.eof()) // while NOT end of file do this + { + tournamentNew = new Tournament(); + tournamentNew->readFromFileTournaments(infile); + gTournaments.push_back(tournamentNew); + } + + infile.close(); + } else { + cout << "\n\n\t\t.... CAN NOT FIND FILE: TOURNAMENTDATA.DTA ....\n\n\n"; + } + + infile.close(); +} \ No newline at end of file diff --git a/src/kode/writeToFile.cpp b/src/kode/writeToFile.cpp new file mode 100644 index 0000000000000000000000000000000000000000..78c1e874fdb64735164377122cf295626860be03 --- /dev/null +++ b/src/kode/writeToFile.cpp @@ -0,0 +1,48 @@ + +#include <iostream> // cout, cin +#include <string> // string +#include <vector> // vector +#include <iomanip> // setw +#include <fstream> // ifstream, ofstream +#include "LesData3.h" +#include "player.h" +#include "Tournament.h" +#include "functions.h" + +using namespace std; + +extern vector<Tournament *> gTournaments; +extern vector<Player*> playerList; +extern vector<Player*> sortedRankings; + + +void writeToFile() { + + ofstream outFile("PlayerList.dta"); + + if(outFile) { + for (int i = 0; i < playerList.size(); i++) { + playerList[i]->writeToFilePlayer(outFile); + } + outFile.close(); + } + + +} + + +void writeToFileTournament() { + + ofstream outfile("tournamentData.dta"); + + if (outfile) + { + for (int i = 0; i < gTournaments.size(); i++) + gTournaments[i]->writeToFileTournament(outfile); + + outfile.close(); + } + else + cout << "\nCouldn't find tournamentData.dta\n"; // maybe unneccesary, considering it's also checked when reading data from file + +} \ No newline at end of file