Brian said:
Could anyone please tell me:
Is there a simple way of reading and writing to a C++ binary file using
a class or structure containing strings.
I know that null terminated character arrays work, but I want
specifically to use data defined as string type.
I seem to get problems reading back anything sent to the file as soon as
I introduce a string into the class.
TIA
==
Brian
Strings fall under the category of variable sized records.
There are two general methods for storing strings:
1. Store the length first, then the data.
2. Store the data followed by a sentinel value.
The C style strings are in format #2 above.
I prefer to use method #1, since I perform a block read after
the quantity is read, rather than reading one character at a
time searching for the sentinel.
Also remember that you cannot use a binary write on a structure
or class containing strings. I suggest that you provide a
method in the class for writing and reading to binary streams.
Search the web for "C++ persistence serialize". The process
of storing classes to a data store and retriving them is often
called "persistence" or "serializing". You can search this
newsgroup using the keywords also.
--
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
http://www.sgi.com/tech/stl -- Standard Template Library