Zapisanie obiektu do pliku

0

Mam problem z zapisaniem wektora do pliku. Kombinowalem troche z przeciazaniem operatorow ale nie wychodzilo mi to tak jakbym chcial. Czy moglby mnie ktos nakierowac co zrobic, zeby po dodaniu obiektu do wektora obiektow, obiekt ten automatycznie byl dopisywany do pliku? Na te chwile w pliku siedzi jeden obiekt, a dodawanie wiekszej ilosci juz sie nie wykonuje.

#ifndef BOOK_H
#define BOOK_H
#include<string>
#include<vector>
#include<iostream>
#include<fstream>

class book
{
public:
std::string Author, Title;
int Year;
book(){}
book(std::string author, std::string title, int year);
friend struct library;
friend std::ostream& operator<<(std::ostream& out, book const& book);
~book(){}
};

struct library
{
public:
friend class book;
std::vector<book> books;
void add_book(const book& value);
};


#endif // BOOK_H 
 #include "book.h"

book::book(std::string author, std::string title, int year)
:Author(author), Title(title), Year(year){}

std::ostream& operator<<(std::ostream& out, book const& book)
{
return out<<book.Author<<book.Title<<book.Year;
}

void library::add_book(const book& value)
{
std::fstream file;
file.open("data.txt", std::ios::app);
books.push_back(value);
file<<value;
file.close();
}
1
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

class cObj{
    public:
        cObj(string Data1, string Data2, string Data3);
        string sData1;
        string sData2;
        string sData3;
};
cObj::cObj(string Data1, string Data2, string Data3){
    sData1 = Data1;
    sData2 = Data2;
    sData3 = Data3;
}

vector <cObj *> vVector;
int main()
{
    vVector.push_back(new cObj("1", "2", "3"));
    vVector.push_back(new cObj("4", "5", "6"));
    vVector.push_back(new cObj("7", "8", "9"));
    vVector.push_back(new cObj("10", "11", "12"));

    fstream iFile("data.txt", ios::out);
    for(auto &Obj : vVector)
      iFile << Obj->sData1 << " " << Obj->sData2 << " " << Obj->sData3 << "\n";
    iFile.close();
}
 
4
class cObj{
    public:
        cObj(string Data1, string Data2, string Data3);
        string sData1;
        string sData2;
        string sData3;
};
cObj::cObj(string Data1, string Data2, string Data3){
    sData1 = Data1;
    sData2 = Data2;
    sData3 = Data3;
}
 
vector <cObj> vVector;
int main()
{
    vVector.emplace_back("1", "2", "3");
    vVector.emplace_back("4", "5", "6");
    vVector.emplace_back("7", "8", "9");
    vVector.emplace_back("10", "11", "12");
 
    fstream iFile("data.txt", ios::out);
    for(auto &Obj : vVector)
      iFile << Obj.sData1 << " " << Obj.sData2 << " " << Obj.sData3 << "\n";
}
0

Cholera ...

2

@gswidwa Jeszcze inna uwaga: niepotrzebny Ci ten user-defined-ctor. Definiując go nie korzystasz z możliwości generowania wielu rzeczy przez kompilator bo Twoja struktura nie jest wtedy PODem. Rozumiem, że to przykład ale imo dobra praktyka jest robienie PODow tam gdzie się da. Do poczytania http://en.cppreference.com/w/cpp/concept/PODType

1 użytkowników online, w tym zalogowanych: 0, gości: 1