1

I was trying to write X objects in a file, to read it later, but I don't understand much about I/O in c++. X would be a class with a string and a number, just as an example. (I used a string, but it is not supposed to me just a letter)

#include <iostream>
#include <string>
#include <sstream>

class X { 
    int _number;
    std::string _letter;
    public:
    X() { _number = 0; _letter = "anonimo"; }
    X(int number, std::string letter) { _number = number; _letter = letter; }
    int getnumber() { return _number; }
    std::string getletter() { return _letter; }
    void setNumber(int number) { _number = number; }
    void setLetter(std::string letter) { _letter = letter; }

};

std::istream& operator>>(std::istream& is, X &x) {
    std::string str;
    std::string letter;
    int number;
    std::getline(is, str);
    sscanf(str.c_str(), "%[^,], %d", letter, &number);A
    X c(number, letter);
    return is;
}
std::ostream& operator<<(std::ostream& out, X &x) {
    out << x.getLetter() << ", " << x.getNum();
    return out;
}

int main(int argc,char *argv[]) {
    X c1 = X(2, "w");
    X c2 = X(6, "c");
    X c3, c4;
    std::ofstream outFile;
    if (!(argc > 1)) return -1;
    outFile.open(argv[1]);
    std::stringstream str;
    str << c1 << std::endl << c2 << std::endl;
    std::string xs = xs.c_str();
    outFile << xs.c_str();
    outFile.close()
    std::ifstream infile;
    infile.open(argv[1]);
    infile >> c3;
    infile >> c4;
    return 0;
   }

I know I'm doing it wrong, and I wanted to know the best way to do it, using std::stringstream to save it in a file, and creating 2 new X's, c3 and c4, using information from c1 and c2.

2 Answers 2

2

The problem you are describing is that of serialization. From the C++ FAQ:

It [serialization] lets you take an object or group of objects, put them on a disk or send them through a wire or wireless transport mechanism, then later, perhaps on another computer, reverse the process: resurrect the original object(s). The basic mechanisms are to flatten object(s) into a one-dimensional stream of bits, and to turn that stream of bits back into the original object(s).

Also from the C++ FAQ, your specific situation appears to match: [36.7] How do I serialize objects that aren't part of an inheritance hierarchy and that don't contain pointers to other objects?

There are lots of questions on SO with respect to serialization. For a broad discussion of the topic, see How do you serialize an object in C++?

Finally, if you choose to go with a text format for serialization, you might consider formatting that text as JSON or XML. For JSON serialization, see Converting C++ class to JSON. For XML serialization, see Minimal XML library for C++?, what's the easiest way to generate xml in c++? and a third-party tool such as CodeSynthesis XSD.

Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest using boost::serialization

http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/index.html

The discussion in these help documents also covers a lot of the considerations around saving objects in files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.