From 4e7443634b5b61f61bab227afbaba29741cfacc6 Mon Sep 17 00:00:00 2001
From: Loke Nesse Svelland <lokens@stud.ntnu.no>
Date: Thu, 28 Apr 2022 16:21:14 +0200
Subject: [PATCH] started on write/read to/from files

---
 src/kode/Tournament.cpp   | 28 ------------------
 src/kode/checkmate.cpp    |  9 +++---
 src/kode/functions.h      |  4 +++
 src/kode/readFromFile.cpp | 62 +++++++++++++++++++++++++++++++++++++++
 src/kode/writeToFile.cpp  | 48 ++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 32 deletions(-)
 create mode 100644 src/kode/readFromFile.cpp
 create mode 100644 src/kode/writeToFile.cpp

diff --git a/src/kode/Tournament.cpp b/src/kode/Tournament.cpp
index dad8d27..ed098c0 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 1583b50..045802b 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 e319466..3c71103 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 0000000..abb802f
--- /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 0000000..78c1e87
--- /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
-- 
GitLab