Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Binary vs. formatted std::string reading/writing to streams

Reply
Thread Tools

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

 
 
Leslaw Bieniasz
Guest
Posts: n/a
 
      01-15-2010

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: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
 
Reply With Quote
 
 
 
 
Eric Böse-Wolf
Guest
Posts: n/a
 
      01-15-2010
Hi Leslaw,

Leslaw Bieniasz <(E-Mail Removed)> 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/i...tors/noskipws/

Yours sincerely,

Eric
 
Reply With Quote
 
 
 
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      01-15-2010
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: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
 
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
text and binary streams subramanian100in@yahoo.com, India C Programming 4 08-24-2008 11:14 PM
Dual binary/character streams? Adam Warner Java 18 11-08-2005 02:57 AM
Writing formatted data (numeric) to a binary file Trevor.Dhu@hotmail.com Python 3 09-02-2005 06:19 AM
Detecting binary verses text file streams Tron Thomas C++ 3 11-08-2004 10:07 PM
Equivalent of fprintf's -20.15g formatting with C++ formatted streams ? Koen C++ 1 10-05-2004 01:53 PM



Advertisments