Write the individual members to your output stream, or have the structure do this, or write the individual members to a buffer:
struct Part_record
{
char id_no[3];
int qoh;
string desc;
double price:
// Block I/O methods
size_t Size_On_Stream(void) const
{
size_t size = 0;
size = sizeof(id_no) + sizeof(goh) + sizeof(price);
size += descr.length() + 1; // +1 for terminating null character
return size;
}
void Store_To_Buffer(unsigned char *& p_buffer) const
{
std::copy((unsigned char *)&id_no[0], (unsigned char *)&id_no[3], p_buffer);
p_buffer += sizeof(id_no);
std::copy((unsigned char *)&goh, (unsigned char *)(&goh) + sizeof(goh), p_buffer);
p_buffer += sizeof(goh);
std::copy((unsigned char *)&price, (unsigned char *)(&price) + sizeof(price), p_buffer);
p_buffer += sizeof(price);
strcpy(p_buffer, descr.str());
p_buffer += descr.length();
*p_buffer = 0x00;
++p_buffer;
return;
}
void Write_To_Stream(ostream& output) const
{
size_t buffer_size = Size_On_Stream();
unsigned char * buffer = new unsigned char [buffer_size];
unsigned char * p_buffer = buffer;
Store_To_Buffer(p_buffer);
output.write((char *)buffer, buffer_size);
delete [] buffer;
return;
}
};
Since you have floating point values, integer values and text, I highly suggest you use an ASCII or text based format such as CSV or XML. Binary versions of numbers (integral and floating point) may not be compatible across platforms, between OS versions or even compiler versions. Also, variable length text is a pain to deal with in binary formats.