0

I have a class like this:

class C {
protected:
    std::string data1;
    int data2;
    int data3;
    std::string data4;
public:
    C() {};
    C(C const *obj) {};
    virtual ~C() {};
};

And another class that is a list of C objects like this:

class CList{
private:
    size_t size;
    C** clist;
public:
    CList();
    ~CList();

What I would like to do is if I have a list with for example 3 C objects, I would like to write the datas to a .txt file so it will look like this: "data1 data2 data3 data4\n data1 data2 data3 data4\n ...." And also if I already have a few saved objects in the .txt file, how can I read them from the file and make a list from them? I know there's a bunch of questions regarding this problem, but I couldn't use them successfully to make mine functional. Do I have to overload the >>, << operators, and if so, how should I do that?

  • Have you tried anything? You add your classes, but no attempt to solve the problem by yourself. – Valentino May 18 '19 at 19:50
  • Possible duplicate of [Is it possible to serialize and deserialize a class in C++?](https://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c) – Quimby May 18 '19 at 19:55
  • You don't **have** to overload the >> and << operators. That's just convention. Do one thing at a time, work out how to read and write your objects first, only then put that code inside >> and << operators. There's no magic in overloading these operators. – john May 18 '19 at 20:07
  • Start small. "How do you write a single string to a file". Then "How do you read a single string from a file." Then "write a string and an int to a file." Then "read a string and an int from a file." – Eljay May 18 '19 at 21:24
  • Hey! As I said, I knew a bunch of questions like this were already posted, and again I apologize for that, but I'm fairly new to C++, I/O especially, and also english isn't my first language so I was struggling to make my code functional based on other questions. However, your comments helped me and I already managed to complete writing to a file, and working on reading from it. Thank you all for your advices to "start small". It really was the solution to try these things with single line of strings. – Somogyi Ármin May 19 '19 at 09:48

0 Answers0