Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an object to file in C++

I have an object with several text strings as members. I want to write this object to the file all at once, instead of writing each string to file. How can I do that?

like image 807
neuromancer Avatar asked Mar 04 '10 00:03

neuromancer


People also ask

How do you write an object to a file in C++?

Writing and Reading class objectswrite( (char*)&Emp_1, sizeof(Emp1) ); Here data present in class object Emp_1 is written to file Employee. txt by calling write function. (char*)&Emp_1 is used to point at the start of an object and sizeof(Emp_1) calculates the number of bytes copied in file.

How do you create a file in C?

To create a file in a 'C' program following syntax is used, FILE *fp; fp = fopen ("file_name", "mode"); In the above syntax, the file is a data structure which is defined in the standard library. fopen is a standard function which is used to open a file.

What is the use of object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

How do I run a .o file?

An object file ( .o ) is not executable. You want to be running ./demo_fully_homomorphic (e.g. the file without extension). Make sure you have execute permissions ( chmod a+x demo_fully_homomorphic ). Show activity on this post.


2 Answers

You can override operator>> and operator<< to read/write to stream.

Example Entry struct with some values:

struct Entry2
{
    string original;
    string currency;

    Entry2() {}
    Entry2(string& in);
    Entry2(string& original, string& currency)
        : original(original), currency(currency)
    {}
};


istream& operator>>(istream& is, Entry2& en);
ostream& operator<<(ostream& os, const Entry2& en);

Implementation:

using namespace std;

istream& operator>>(istream& is, Entry2& en)
{
    is >> en.original;
    is >> en.currency;
    return is;
}

ostream& operator<<(ostream& os, const Entry2& en)
{
    os << en.original << " " << en.currency;
    return os;
}

Then you open filestream, and for each object you call:

ifstream in(filename.c_str());
Entry2 e;
in >> e;
//if you want to use read: 
//in.read(reinterpret_cast<const char*>(&e),sizeof(e));
in.close();

Or output:

Entry2 e;
// set values in e
ofstream out(filename.c_str());
out << e;
out.close();

Or if you want to use stream read and write then you just replace relevant code in operators implementation.

When the variables are private inside your struct/class then you need to declare operators as friend methods.

You implement any format/separators that you like. When your string include spaces use getline() that takes a string and stream instead of >> because operator>> uses spaces as delimiters by default. Depends on your separators.

like image 154
stefanB Avatar answered Oct 25 '22 17:10

stefanB


It's called serialization. There are many serialization threads on SO.

There are also a nice serialization library included in boost.

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

basically you can do

myFile<<myObject 

and

myFile>>myObject

with boost serialization.

like image 27
Chris H Avatar answered Oct 25 '22 17:10

Chris H