I wanna write a program that storing the following struct of data into the binary file:
I was noticed that the byte returned by sizeof(string) being 28, even though the string variable contains more then 28 characters. I tried the following:
But next time when i retrieve by using function read(char *, size_t), the string fields are not what i expected.
So I break out the above structure by separating it from string fields, like this:
I use the following way to collecting data and write to tge file:
Any better solution, i can't found any useful book for the I/O for it.
Code:
struct staff {
string id; // maximum 8 characters.
string name; // maximum 35 characters.
string ic; // identity card number, maximum 14 character.
unsigned short age;
string remark; // no limit.
} employee;
I was noticed that the byte returned by sizeof(string) being 28, even though the string variable contains more then 28 characters. I tried the following:
Code:
write((const char *)&employee, sizeof(employee));
But next time when i retrieve by using function read(char *, size_t), the string fields are not what i expected.
So I break out the above structure by separating it from string fields, like this:
Code:
struct staff1 {
unsigned id;
unsigned name;
unsigned ic;
unsigned remark;
unsigned short age;
} employee;
struct {
string id; // maximum 8 characters.
string name; // maximum 35 characters.
string ic; // identity card number, maximum 14 character.
string remark; // no limit.
} s;
I use the following way to collecting data and write to tge file:
Code:
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
struct staff1 {
unsigned id;
unsigned name;
unsigned ic;
unsigned remark;
unsigned short age;
} employee;
struct {
string id; // maximum 8 characters.
string name; // maximum 35 characters.
string ic; // identity card number, maximum 14 character.
string remark; // no limit.
} s;
int main() {
if (0) {
ofstream oStrF("str.dat", ios::out | ios::binary);
ofstream oEmployeeF("employee.dat", ios::out | ios::binary);
s.id = "01010101";
employee.id = (s.id.length() + 1); // extra byte for null terminator.
s.name = "ABC";
employee.name = (s.name.length() + 1); // extra byte for null terminator.
s.ic = "801010-12-5165";
employee.ic = (s.ic.length() + 1); // extra byte for null terminator.
s.remark = "OK";
employee.remark = (s.remark.length() + 1); // extra byte for null terminator.
employee.age = 21;
oStrF.write((const char *)s.id.c_str(), employee.id);
oStrF.write((const char *)s.name.c_str(), employee.name);
oStrF.write((const char *)s.ic.c_str(), employee.ic);
oStrF.write((const char *)s.remark.c_str(), employee.remark);
oEmployeeF.write((const char *)&employee, sizeof(employee));
}
else {
ifstream iStrF("str.dat", ios::in | ios::binary);
ifstream iEmployeeF("employee.dat", ios::in | ios::binary);
iEmployeeF.read((char *)&employee, sizeof(employee));
// id.
char *buf = new char[employee.id];
iStrF.read((char *)buf, employee.id);
cout << "ID: " << buf << endl;
delete buf;
// name.
buf = new char[employee.name];
iStrF.read((char *)buf, employee.name);
cout << "NAME: " << buf << endl;
delete buf;
// ic.
buf = new char[employee.ic];
iStrF.read((char *)buf, employee.ic);
cout << "IC: " << buf << endl;
delete buf;
// remark.
buf = new char[employee.remark];
iStrF.read((char *)buf, employee.remark);
cout << "REMARK: " << buf << endl;
delete buf;
cout << employee.age << endl;
}
}
Any better solution, i can't found any useful book for the I/O for it.