String Question

Discussion in 'C++' started by Brian Ward, Jan 17, 2004.

  1. Brian Ward

    Brian Ward Guest

    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
     
    Brian Ward, Jan 17, 2004
    #1
    1. Advertising

  2. "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.


    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
     
    Victor Bazarov, Jan 18, 2004
    #2
    1. Advertising

  3. 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
     
    Thomas Matthews, Jan 18, 2004
    #3
  4. Brian Ward

    Jon Bell Guest

    In article <>,
    Brian Ward <> 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 <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Jan 19, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    0
    Views:
    757
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,686
    Mladen Adamovic
    Dec 5, 2003
  3. Matt
    Replies:
    3
    Views:
    531
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Bruce Sam
    Replies:
    15
    Views:
    7,989
    John C. Bollinger
    Nov 19, 2004
  5. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,808
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
Loading...

Share This Page