diff --git a/functions.cpp b/functions.cpp index b7853f309dfd94cdd12714cdf563e34524c2978d..978d72f859665ffc4f4fce687d5f7c2826640ecc 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 4d512b22b580f4954272a00e7ec3eea7fc1288a6..abdd52126f07fd476dce509dfce0ce26cced4b58 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 8409859b6ec9a9249f8c81f333f8e67cbc717ae4..509aacdc8b2e7452aa8a9ae6639dd1775ed52bc2 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; }