L
Leslaw Bieniasz
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
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