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.