String Question

B

Brian Ward

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
 
V

Victor Bazarov

Brian Ward 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.

There probably is.
I know that null terminated character arrays work, but I want
specifically to use data defined as string type.

The 'std::string' type actually contains the data you're so used to.
You only need to use c_str() member to gain access to it.
I seem to get problems reading back anything sent to the file as soon as
I introduce a string into the class.

Read FAQ 5.8 please.

Victor
 
T

Thomas Matthews

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
 
J

Jon Bell

Is there a simple way of reading and writing to a C++ binary file using
a class or structure containing strings.

Depends on your definition of "simple." :)
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.

An object of type 'string' does not directly contain the character data.
Instead, it contains a pointer (or pointers) to a dynamically allocated
structure that actually contains the characters. If you try to simply
write the class or struct data out using a binary write(), you'll write
the pointers but not the data itself.

You need to define some sort of file format which allows for the
character data, and then write it out in a separate operation from the
main body of the struct. Then when you read the stuff back in, use the
character data to initialize a std::string in the new object.
 

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

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top