Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String Question

Reply
Thread Tools

String Question

 
 
Brian Ward
Guest
Posts: n/a
 
      01-17-2004
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-18-2004
"Brian Ward" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      01-18-2004
Brian Ward wrote:
> 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

 
Reply With Quote
 
Jon Bell
Guest
Posts: n/a
 
      01-19-2004
In article <(E-Mail Removed)>,
Brian Ward <(E-Mail Removed)> wrote:
>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.

--
Jon Bell <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Is "String s = "abc";" equal to "String s = new String("abc");"? Bruce Sam Java 15 11-19-2004 06:03 PM
String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"}; Matt Java 3 09-17-2004 10:28 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments