Newbie: how to write a struct to a file?

Z

Zalek Bloom

Hello,

I have defined a "struct".

struct Record

{
public:
char Fname[20];
char Lname[20];
int Salary;
}

Is its possible to write a Record to a file instead parsing it to
individual members and writing Fname, Lname and Salary?

Thanks,

Zalek
 
T

Thomas Matthews

Zalek said:
Hello,

I have defined a "struct".

struct Record

{
public:
char Fname[20];
char Lname[20];
int Salary;
}

Is its possible to write a Record to a file instead parsing it to
individual members and writing Fname, Lname and Salary?

Thanks,

Zalek

Try this:
ofstream data_file("my_data.bin", ios_base::binary);
Record data;
data_file.write((unsigned char *) &data, sizeof Record);

The above technique assumes that the char arrays are fixed
size and it writes it in unformatted, binary mode.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Attila Feher

Thomas Matthews wrote:
[SNIP]
Try this:
ofstream data_file("my_data.bin", ios_base::binary);
Record data;
data_file.write((unsigned char *) &data, sizeof Record);

The above technique assumes that the char arrays are fixed
size and it writes it in unformatted, binary mode.

And will possibly not be able to read it back on another architecture.
 
S

Shane Beasley

Attila Feher said:
Thomas Matthews wrote:
[SNIP]
Try this:
ofstream data_file("my_data.bin", ios_base::binary);
Record data;
data_file.write((unsigned char *) &data, sizeof Record);

The above technique assumes that the char arrays are fixed
size and it writes it in unformatted, binary mode.

And will possibly not be able to read it back on another architecture.

....or the same architecture with another compiler, or the same
architecture and compiler with different compiler options.

The FAQ has a little bit about this, although it's less about
portability aspects and more about how to output whole data
structures:

<http://www.parashift.com/c++-faq-lite/serialization.html>

If you care at all about portability, you might first consider a
purely textual format (XML seems popular these days), possibly
compressed (e.g., via gzip). If you find that too bulky and/or slow
for your purposes, standardize on a binary format (e.g., 32-bit
little-endian integer, 64-bit IEEE floating point number, strings with
leading length indicator, etc.) and figure out how to portably read
and write in that format -- taking into account all supported
combinations of architecture, compiler, and compiler options, of
course.

- Shane
 
S

Sam Holden

If you care at all about portability, you might first consider a
purely textual format (XML seems popular these days), possibly
compressed (e.g., via gzip). If you find that too bulky and/or slow
for your purposes, standardize on a binary format (e.g., 32-bit
little-endian integer, 64-bit IEEE floating point number, strings with
leading length indicator, etc.) and figure out how to portably read
and write in that format -- taking into account all supported
combinations of architecture, compiler, and compiler options, of
course.

There is a *large* amount of ground between XML and a binary format.

Parsing XML can add significant overhead (both time and space). A
simple text format will often be *considerably* faster (though less
useful if having a user editable file is a good thing).

Of course a binary file will be faster again, but often the gap between
XML and plain old text is bigger than the gap between plain old text
and a binary format.
 
D

Default User

Zalek said:
Hello,

I have defined a "struct".

struct Record

{
public:
char Fname[20];
char Lname[20];
int Salary;
}

Is its possible to write a Record to a file instead parsing it to
individual members and writing Fname, Lname and Salary?


About 90% of it's text anyway, so why worry about it. As long as there
are no floating point numbers, you don't have to worry about loss of
precision. Go with the text. Makes it easier for you to review the data
files too.




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top