diff --git a/Main.cpp b/Main.cpp index a9a51a9..297c258 100644 --- a/Main.cpp +++ b/Main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -99,17 +100,43 @@ typedef struct contacts{ } Kontaktdaten; -void addContact(Kontaktdaten* person, std::vector &Telefonbuch) { - Telefonbuch.push_back(person); +void addContact(Kontaktdaten* person, std::vector *Telefonbuch) { + Telefonbuch->push_back(person); + // std::vector buch = *Telefonbuch; + // buch.push_back(person); } -void printAllContacts(std::vector Telefonbuch) { - for (Kontaktdaten * personpointer: Telefonbuch) { +void printAllContacts(std::vector *Telefonbuch) { + for (Kontaktdaten * personpointer: *Telefonbuch) { Kontaktdaten person = *personpointer; std::cout << person.firstName << " " << person.lastName << " " << person.number << std::endl; } } +void saveAllContacts(std::vector *Telefonbuch, std::string *fileName) { + std::ofstream outfile; + outfile.open(*fileName); + + for (Kontaktdaten * personpointer: *Telefonbuch) { + Kontaktdaten person = *personpointer; + outfile << person.firstName << std::endl << person.lastName << std::endl << person.number << std::endl; + } + + outfile.close(); +} + +void readContactsFromFile(std::vector *Telefonbuch, std::string *fileName) { + std::ifstream infile; + infile.open(*fileName); + Kontaktdaten person; + infile >> person.firstName >> person.lastName; + infile >> person.number; + + + infile.close(); + addContact(&person, Telefonbuch); +} + /* Kontaktdaten * createContact(std::string firstName, std::string lastName, int number) { Kontaktdaten person = new Kontaktdaten; @@ -123,11 +150,12 @@ Kontaktdaten * createContact(std::string firstName, std::string lastName, int nu */ void TelefonbuchMain() { + std::string fileName = "../IO-Files/Telefonbuch.txt"; std::vector Telefonbuch; - Kontaktdaten * Robin = new Kontaktdaten; - Kontaktdaten * Saxo = new Kontaktdaten; - Kontaktdaten * Jesus = new Kontaktdaten; + auto * Robin = new Kontaktdaten; + auto * Saxo = new Kontaktdaten; + auto * Jesus = new Kontaktdaten; Robin->number = 123456; Robin->firstName = "Robin"; @@ -141,15 +169,16 @@ void TelefonbuchMain() { Jesus->firstName = "Jesus"; Jesus->lastName = "Nazarett"; - addContact(Robin, Telefonbuch); - addContact(Saxo, Telefonbuch); - addContact(Jesus, Telefonbuch); + addContact(Robin, &Telefonbuch); + addContact(Saxo, &Telefonbuch); + addContact(Jesus, &Telefonbuch); - printAllContacts(Telefonbuch); + printAllContacts(&Telefonbuch); + saveAllContacts(&Telefonbuch, &fileName); - for (Kontaktdaten * personpointer: Telefonbuch) { - delete personpointer; - } + delete Robin; + delete Saxo; + delete Jesus; } #pragma endregion