Binary vs. formatted std::string reading/writing to streams

Discussion in 'C++' started by Leslaw Bieniasz, Jan 15, 2010.

  1. Hi,

    I need to write and read std::strings as whole objects
    from/to std::streams, either in a formatted or binary way.
    Are there any built-in mechanisms for doing this (any global
    functions or class methods in std::string)?
    I cannot find any.

    Formatted writing seems easy, for example:

    std::eek:stream outstr;
    std::string str;
    outstr << str;

    outputs entire str to outstr. However, something like

    std::instream instr;
    std::string str;
    instr >> str;

    gets only the first continuous sub-string contained in
    instr into str. The sub-string may not be identical to
    the entire string that was earlier written to the stream
    from which the reading is performed.

    What I need is a possibility to save an std::string into a stream/file
    and then re-read the entire string from the same stream/file,
    even if the string contained separate sub-strings.
    Perhaps some sort of binary reading/writing can be applied?

    A related question is:

    Is this legal to open streams in binary mode, and use operators
    << and >> to read or write from these streams? For example,
    if I open a binary stream, can I save (and later correctly retrieve)
    some objects in it using binary writing, and some other objects using
    formatted writing employing operators << and >> ?

    Leslaw
    Leslaw Bieniasz, Jan 15, 2010
    #1
    1. Advertising

  2. Hi Leslaw,

    Leslaw Bieniasz <> writes:

    > gets only the first continuous sub-string contained in
    > instr into str. The sub-string may not be identical to
    > the entire string that was earlier written to the stream
    > from which the reading is performed.


    What do you mean by "substring"? Are "substrings" delimited by
    whitespace? Then you might take a look at:

    http://www.cplusplus.com/reference/iostream/manipulators/noskipws/

    Yours sincerely,

    Eric
    Eric Böse-Wolf, Jan 15, 2010
    #2
    1. Advertising

  3. Am 15.01.2010 11:42, schrieb Leslaw Bieniasz:
    > I need to write and read std::strings as whole objects
    > from/to std::streams, either in a formatted or binary way.
    > Are there any built-in mechanisms for doing this (any global
    > functions or class methods in std::string)?
    > I cannot find any.
    >
    > Formatted writing seems easy, for example:
    >
    > std::eek:stream outstr;
    > std::string str;
    > outstr << str;
    >
    > outputs entire str to outstr. However, something like
    >
    > std::instream instr;
    > std::string str;
    > instr >> str;
    >
    > gets only the first continuous sub-string contained in
    > instr into str.


    nistr >> str; stop reads until it finds a whitespace.

    If you want to read a line of text, use std::getline:

    std::getline(instr, str);

    This function reads until it find a '\n' character in the stream. For
    another delimiter, there's also a three argument version of that
    function where the third parameter is the delimiter:
    http://www.cplusplus.com/reference/string/getline/

    --
    Thomas
    Thomas J. Gritzan, Jan 15, 2010
    #3
    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. Koen
    Replies:
    1
    Views:
    580
    Victor Bazarov
    Oct 5, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,294
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    3
    Views:
    586
    Dennis Lee Bieber
    Sep 2, 2005
  4. Jeffrey Walton
    Replies:
    10
    Views:
    937
    Mathias Gaunard
    Nov 26, 2006
  5. mpurdy
    Replies:
    4
    Views:
    459
    mpurdy
    Apr 9, 2011
Loading...

Share This Page