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 <vector>
|
||||
|
||||
@ -99,17 +100,43 @@ typedef struct contacts{
|
||||
} Kontaktdaten;
|
||||
|
||||
|
||||
void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> &Telefonbuch) {
|
||||
Telefonbuch.push_back(person);
|
||||
void addContact(Kontaktdaten* person, std::vector<Kontaktdaten*> *Telefonbuch) {
|
||||
Telefonbuch->push_back(person);
|
||||
// std::vector<Kontaktdaten *> buch = *Telefonbuch;
|
||||
// buch.push_back(person);
|
||||
}
|
||||
|
||||
void printAllContacts(std::vector<Kontaktdaten*> Telefonbuch) {
|
||||
for (Kontaktdaten * personpointer: Telefonbuch) {
|
||||
void printAllContacts(std::vector<Kontaktdaten*> *Telefonbuch) {
|
||||
for (Kontaktdaten * personpointer: *Telefonbuch) {
|
||||
Kontaktdaten person = *personpointer;
|
||||
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 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<Kontaktdaten*> 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
|
||||
|
Loading…
Reference in New Issue
Block a user