Add saving to file
This commit is contained in:
parent
ef2bb1ddc6
commit
290c9e2a53
57
Main.cpp
57
Main.cpp
@ -1,3 +1,4 @@
|
|||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -99,17 +100,43 @@ typedef struct contacts{
|
|||||||
} Kontaktdaten;
|
} Kontaktdaten;
|
||||||
|
|
||||||
|
|
||||||
void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> &Telefonbuch) {
|
void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> *Telefonbuch) {
|
||||||
Telefonbuch.push_back(person);
|
Telefonbuch->push_back(person);
|
||||||
|
// std::vector<Kontaktdaten *> buch = *Telefonbuch;
|
||||||
|
// buch.push_back(person);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printAllContacts(std::vector<Kontaktdaten*> Telefonbuch) {
|
void printAllContacts(std::vector<Kontaktdaten*> *Telefonbuch) {
|
||||||
for (Kontaktdaten * personpointer: Telefonbuch) {
|
for (Kontaktdaten * personpointer: *Telefonbuch) {
|
||||||
Kontaktdaten person = *personpointer;
|
Kontaktdaten person = *personpointer;
|
||||||
std::cout << person.firstName << " " << person.lastName << " " << person.number << std::endl;
|
std::cout << person.firstName << " " << person.lastName << " " << person.number << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void saveAllContacts(std::vector<Kontaktdaten*> *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<Kontaktdaten*> *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 * createContact(std::string firstName, std::string lastName, int number) {
|
||||||
Kontaktdaten person = new Kontaktdaten;
|
Kontaktdaten person = new Kontaktdaten;
|
||||||
@ -123,11 +150,12 @@ Kontaktdaten * createContact(std::string firstName, std::string lastName, int nu
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
void TelefonbuchMain() {
|
void TelefonbuchMain() {
|
||||||
|
std::string fileName = "../IO-Files/Telefonbuch.txt";
|
||||||
std::vector<Kontaktdaten*> Telefonbuch;
|
std::vector<Kontaktdaten*> Telefonbuch;
|
||||||
|
|
||||||
Kontaktdaten * Robin = new Kontaktdaten;
|
auto * Robin = new Kontaktdaten;
|
||||||
Kontaktdaten * Saxo = new Kontaktdaten;
|
auto * Saxo = new Kontaktdaten;
|
||||||
Kontaktdaten * Jesus = new Kontaktdaten;
|
auto * Jesus = new Kontaktdaten;
|
||||||
|
|
||||||
Robin->number = 123456;
|
Robin->number = 123456;
|
||||||
Robin->firstName = "Robin";
|
Robin->firstName = "Robin";
|
||||||
@ -141,15 +169,16 @@ void TelefonbuchMain() {
|
|||||||
Jesus->firstName = "Jesus";
|
Jesus->firstName = "Jesus";
|
||||||
Jesus->lastName = "Nazarett";
|
Jesus->lastName = "Nazarett";
|
||||||
|
|
||||||
addContact(Robin, Telefonbuch);
|
addContact(Robin, &Telefonbuch);
|
||||||
addContact(Saxo, Telefonbuch);
|
addContact(Saxo, &Telefonbuch);
|
||||||
addContact(Jesus, Telefonbuch);
|
addContact(Jesus, &Telefonbuch);
|
||||||
|
|
||||||
printAllContacts(Telefonbuch);
|
printAllContacts(&Telefonbuch);
|
||||||
|
saveAllContacts(&Telefonbuch, &fileName);
|
||||||
|
|
||||||
for (Kontaktdaten * personpointer: Telefonbuch) {
|
delete Robin;
|
||||||
delete personpointer;
|
delete Saxo;
|
||||||
}
|
delete Jesus;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma endregion
|
#pragma endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user