From 0a121e66919702f39f854c0c90d9e60ad0764a77 Mon Sep 17 00:00:00 2001 From: Eraser <erayduezenli@googlemail.com> Date: Thu, 13 Apr 2023 15:36:58 +0200 Subject: [PATCH] Aufgabe 4 done --- functions.cpp | 26 ++++++++++++++++++++++++-- functions.h | 2 ++ main.cpp | 8 ++++++-- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/functions.cpp b/functions.cpp index b7853f3..978d72f 100644 --- a/functions.cpp +++ b/functions.cpp @@ -8,7 +8,7 @@ #include <sstream> #include <vector> -//std::list<Cards> getListOfCards(const std::string& filename) +// Aufgabe 3 std::list<Cards> getListOfCards(const std::string& filename) { std::ifstream file(filename); if(!file) { @@ -64,4 +64,26 @@ std::list<Cards> getListOfCards(const std::string& filename) { //return the cards as a list return CardsList; -} \ No newline at end of file +} + + +// Aufgabe 4 + +void saveCardsToFile(const std::list<Cards>& cardsList) { + //Create new file + std::ofstream file("scrambledNew.txt"); + + //Check if file can be opened + if(!file) { + throw std::runtime_error("Error: Unable to open file."); + } + //Print every card object in CardsList into separate Line, add the vertical bar again + for (const auto& card : cardsList) { + file << card.name << "|" << card.mana << "|" << card.cmc << "|" << card.type << "|" << card.count << std::endl; + } + + //Close file + file.close(); +} + + diff --git a/functions.h b/functions.h index 4d512b2..abdd521 100644 --- a/functions.h +++ b/functions.h @@ -9,6 +9,8 @@ std::list<Cards> getListOfCards(const std::string& filename); +void saveCardsToFile(const std::list<Cards>& CardsList); + diff --git a/main.cpp b/main.cpp index 8409859..509aacd 100644 --- a/main.cpp +++ b/main.cpp @@ -18,9 +18,13 @@ int main() { s.name = "peter"; s.type = "uwu"; std::cout << s.name << s.count << s.type << s.cmc << std::endl; - //To-Do, only absolute path works */ - getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); + std::list<Cards> test; + + //TODO relative path doesnt work + test = getListOfCards("/Users/erayduzenli/CLionProjects/magicCards/scrambled.txt"); + //TODO why does it save the file in the debug folder + saveCardsToFile(test); return 0; } -- GitLab