473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

ostream like interface to a string?

Hello,
I was thinking about one solution, but no luck.

suppose I have an std::string object.
I want to be able to use this string like std::ostringstream.
something like:

string str;
ostringstream out(str);

out << "Hello world." << 123;

and then str should contain "Hello world.123". Basicly, I heed a
specified string object be a underlying buffer for a ostringstream
object. How can I do that?

Should I write another class derived from streambuf or something???
Thanks for any ideas.
Jul 22 '05 #1
3 4516
__PPS__ wrote:
Hello,
I was thinking about one solution, but no luck.

suppose I have an std::string object.
I want to be able to use this string like std::ostringstream.
something like:

string str;
ostringstream out(str);

out << "Hello world." << 123;

and then str should contain "Hello world.123". Basicly, I heed a
specified string object be a underlying buffer for a ostringstream
object. How can I do that?

Should I write another class derived from streambuf or something???


Yes, something like that. The least "expensive" (from manhours POV) is to
use ostringstream. I have found that in most cases when I would need that
spec. string looks exactly like the above case of yours, which is
equvivalent to this:
ostringstream ostr();

out << "Hello world." << 123;
string str(ostr.str();

Give a library using COW strings, this is just as efficient (or inefficient
in MT :-)) as using that special wrapper-thing.

--
WW aka Attila
:::
Change is inevitable, except from a vending machine.
Jul 22 '05 #2
> Yes, something like that. The least "expensive" (from manhours POV) is to
use ostringstream. I have found that in most cases when I would need that
spec. string looks exactly like the above case of yours, which is
equvivalent to this:
ostringstream ostr();

out << "Hello world." << 123;
string str(ostr.str();

Give a library using COW strings, this is just as efficient (or inefficient
in MT :-)) as using that special wrapper-thing.


What are the COW strings??
I solved it this way: (but it's not what I needed/wanted)

/////////////
#include <iostream>
#include <sstream>
#include <string>

class xostream : public std::ostringstream {
protected:
std::string & str_;
public:
xostream (std::string & s) :
str_(s),
std::ostringstream(s,std::ios::ate|std::ios::app)
{ }
~xostream ()
{ str_ = str(); }
};

// and then:
using namespace std;

int main(int,char*[]){
string data = "Hello1";
{
xostream file(data);
file << "\nHello2"
<< "\nInt: " << 12
<< "\nFloat: " << 123.3456 << std::endl;
}
std::cout << data << std::endl; //produces unbelievable result !!
return 0;
}
////////

But there seems to be a bug in libraries - it produces different
output when compiled by different compilers:

Microsoft Visual C++ .NET 2002 / VC 6.0 (Incorrect):

Hello2
Int: 12
Float: 123.346

Microsoft Visual C++ 2005 (Correct):
Hello1
Hello2
Int: 12
Float: 123.346

Looks like there are problems with ostringstream in older versions.
to avoid problem:
xostream::xostream (std::string & s) : str_(s),
std::ostringstream(std::ios::ate|std::ios::app)
{ *this << s; }
/**/
as to the "The least "expensive" (from manhours POV)" - It's not work
or anything - it's my own idea (unrelated to job). So, I'm not looking
for easy ways :)
Originally it was to create a virtual filesystem for a small my
private project.
It stored "files" in a std::map<string,string> and there was also an
ebedded Javascript interpreter to run scripts that would have access
to these files. I wanted to provide standart fstream interface to
these virtual files (stored in std::strings) so that they would be
manipulated directly without any unnecessary copying or buffering (and
using this interface, manipulate these files from JS, or from external
modules). So the interface should be based on standard classes...
That's why I posted my question here - someone might have had
implemented something like this and will share ideas/info.
In some articles/tutorials I saw examples using input_iterator , maybe
it could help me??
Thanks.
Jul 22 '05 #3
__PPS__ wrote:
Yes, something like that. The least "expensive" (from manhours POV) is
to use ostringstream. I have found that in most cases when I would
need that spec. string looks exactly like the above case of yours,
which is equvivalent to this:
ostringstream ostr();

out << "Hello world." << 123;
string str(ostr.str();

Give a library using COW strings, this is just as efficient (or
inefficient in MT :-)) as using that special wrapper-thing.


What are the COW strings??
I solved it this way: (but it's not what I needed/wanted)


http://www.gotw.ca/gotw/043.htm

--
WW aka Attila
:::
Water which is too pure has no fish.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexander Stippler | last post by:
Hi, The short story: I have to redirect stdout (not cout!!) to an ostream. How can I manage this? The long story: I use the NAGC numerics library with C++ and want to use its output routines...
1
by: hitech_guy | last post by:
Hi, I am creating my own class derving from ostream. I am facing problem in compiling with GNU G++ compiler under linux class IWCoreEXPORT IWAFTraceLine : public ostream { public:...
13
by: Peteroid | last post by:
These don't work (I'm using VS C++.NET 2005 Express with clr:/pure syntax): ostream& operator <<( ostream& output, String^ str ) { output << str ; //compile error return output ; } ...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
6
by: silversurfer2025 | last post by:
Hello, I am currently trying to derive a class from ostream (which is giving output to my GUI), such that I can give my methods either std::cout or my own outputstream-class to be used as output...
5
by: lars | last post by:
Hi, I have C (printf) style debug functions static void debugPrint(Silver::Strategy& s, unsigned level, const char *format, ...) { if (s.verbosityLevel() >= level ) { va_list args ;...
4
by: Art Werschulz | last post by:
Hi. I would like one of the constructors for a given class to take an ostream as a parameter. The following example (foo.cc) doesn't work: ...
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
8
by: dev_15 | last post by:
Hi, i have the following program to display the input received separated into any word that has Uppercase letters and all lowercase words. For the following input "Upper lower" i get the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.