Skip to content
Snippets Groups Projects
Commit 4e744363 authored by Loke Nesse Svelland's avatar Loke Nesse Svelland
Browse files

started on write/read to/from files

parent 8438afb7
No related branches found
No related tags found
No related merge requests found
...@@ -190,17 +190,7 @@ void Tournament::endTournament() ...@@ -190,17 +190,7 @@ void Tournament::endTournament()
*/ */
void Tournament::writeToFileTournament(ofstream &out) 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) ...@@ -208,23 +198,5 @@ void Tournament::writeToFileTournament(ofstream &out)
*/ */
void Tournament::readFromFileTournaments(ifstream &in) 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
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <vector> // vector #include <vector> // vector
#include <iomanip> // setw #include <iomanip> // setw
#include <fstream> // ifstream, ofstream #include <fstream> // ifstream, ofstream
#include "writeToFile.cpp"
#include "LesData3.h" #include "LesData3.h"
#include "player.h" #include "player.h"
#include "Tournament.h" #include "Tournament.h"
...@@ -27,8 +28,8 @@ int main() ...@@ -27,8 +28,8 @@ int main()
{ {
char command; char command;
//readFromFile(); readFromFile();
//readFromFileTournament(); readFromFileTournament();
do do
{ {
...@@ -90,8 +91,8 @@ int main() ...@@ -90,8 +91,8 @@ int main()
}while (command != 'Q'); }while (command != 'Q');
// writeToFileTournament(); writeToFileTournament();
// writeToFile(); writeToFile();
return 0; return 0;
} }
......
...@@ -16,5 +16,9 @@ void viewPlayers(); //+ ...@@ -16,5 +16,9 @@ void viewPlayers(); //+
void editPlayer(); //+ void editPlayer(); //+
void startNewTournament();//+ void startNewTournament();//+
void deletePlayer();//- void deletePlayer();//-
void readFromFile();
void readFromFileTournament();
void writeToFile();
void WriteToFileTournament();
#endif //CHECKMATE_FUNCTIONS_H #endif //CHECKMATE_FUNCTIONS_H
#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
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment