问题
I am running in to an issue while reading/writing a struct having complex data specifically string. my struct looks like below.
struct MyRecord {
char name[80];
string location; // <<== note this
double balance;
unsigned long account_num;
};
I use different functions for reading and writing.
This is the code I am using to write the data to file
struct MyRecord acc;
strcpy(acc.name, "R");
acc.location = "newlocation ok"; // <<== note this
acc.balance = 1.3;
acc.account_num = 34;
ofstream outbal("balance", ios::out | ios::binary);
outbal.write((char *) &acc, sizeof(struct MyRecord));
same like I am using the below code to read from file.
ifstream inbal("balance", ios::in | ios::binary);
inbal.read((char *) &acc, sizeof(struct MyRecord));
when I compile, it compiles nicely, but when executing I am getting the big list of error messages
./binaryfile_3[0x8048f6a]
./binaryfile_3[0x8048e38]
./binaryfile_3[0x8048e54]
/lib/libc.so.6(__libc_start_main+0xe6)[0x148d26]
./binaryfile_3[0x8048aa1]
======= Memory map: ========
00132000-002c3000 r-xp 00000000 fd:00 918500 /lib/libc-2.12.so
// continues....
although, it works in following 2 conditions.
- Read write are in same function
- If I remove the
string location
I am new to c++, I tried writing using the manual and ended up in error. I searched through several solution and all end up in error while adding the string
property. The above script also I grabbed from a site. I just modified it to match my issue which having different function to read/write and struct having string
Please help me to pin point the issue. Thanks.
回答1:
Add these two functions
std::ostream & operator << ( std::ostream & os, const MyRecord & rec ) {
os.write( rec.name, sizeof( rec.name ) );
size_t sz = rec.location.size();
os.write( & sz, iszeof( sz ) );
os.write( rec.location.c_str(), sz );
os.write( (char*) & balance, sizeof( rec.balance ) );
os.write( (char*) & account_num, sizeof( rec.account_num ) );
return os;
}
std::istream & operator >> ( std::ostream & is, MyRecord & rec ) {
is.read( rec.name, sizeof( rec.name ) );
size_t sz;
is.read( & sz, iszeof( sz ) );
rec.resize( sz );
if ( sz != 0 ) {
is.read( & rec.location[ 0 ], sz );
}
is.read( (char*) & balance, sizeof( rec.balance ) );
is.read( (char*) & account_num, sizeof( rec.account_num ) );
}
And here is a usage
ofstream outbal("balance", ios::out | ios::binary);
outbal << acc;
ifstream inbal("balance", ios::in | ios::binary);
inbal >> acc;
This is poor handmade similarity of Boost.Serialization. Indeed, it would be better to use boost, then my vehicle.
来源:https://stackoverflow.com/questions/22961017/read-write-struct-containing-string-property-to-binary-file