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?
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.
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.
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.)
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.
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 operator
s implementation.
When the variables are private inside your struct/class then you need to declare operator
s 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With