By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

stringstream tie problem

P: n/a
Hi Group,

I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2).

I have a situation where I need to tie an ostringstream
to an istringstream so I can write to one and read back
what I wrote from the other. I had expected the code to
look like:

ostringstream* ostr_stdin = new ostringstream();
istringstream* istr_stdin = new istringstream();

istr_stdin->tie(ostr_stdin);

// this to test only...
// ====================

ostr_stdin->write("www", 3);
char buf[20];
buf[0] = '\0';
*istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;

but that just plain and simple doesn't work! I have also
tried variants like:

istr_stdin->str(ostr_stdin->str());

or something similar with rdbuf(), which the documentation
(so far as such is available), suggests should work. With
the deprecated classes istrstream and ostrstream this was
all no problem, you simply give the same char* to the constructor.
I also tried using the i/ostringstream constructors that take
a string as argument and giving the same string, or ditto
with char*, all to no avail.

Anyone know how to do this?

many thanks,
David
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
David Johnstone wrote:
Hi Group,

I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2).

I have a situation where I need to tie an ostringstream
to an istringstream so I can write to one and read back
what I wrote from the other.
That's not what tying does. If you tie an input stream and an output
stream together, the output stream will be flushed when you read
something from the input stream.
I had expected the code to
look like:

ostringstream* ostr_stdin = new ostringstream();
istringstream* istr_stdin = new istringstream();

istr_stdin->tie(ostr_stdin);

// this to test only...
// ====================

ostr_stdin->write("www", 3);
char buf[20];
buf[0] = '\0';
*istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;

but that just plain and simple doesn't work! I have also
tried variants like:

istr_stdin->str(ostr_stdin->str());
This will copy the string from ostr_stdin to istr_stdin, so they will
contain the same text afterwards, but of course if one changes its
text, the other one doesn't.
or something similar with rdbuf(), which the documentation
(so far as such is available), suggests should work. With
the deprecated classes istrstream and ostrstream this was
all no problem, you simply give the same char* to the constructor.
I also tried using the i/ostringstream constructors that take
a string as argument and giving the same string, or ditto
with char*, all to no avail.


Again, both will get a copy of that string. So if one changes its
string, the other string doesn't change.
Why don't you just use one stringstream? You can use that to read what
you wrote to it:

#include <sstream>
#include <iostream>

int main()
{
std::stringstream str;
std::string something;

str << "Hello";
str >> something;

std::cout << something << '\n';
}

Jul 19 '05 #2

P: n/a
On 13 Nov 2003 01:12:54 -0800, da*******@mail.com (David Johnstone)
wrote:
Hi Group,

I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2).

I have a situation where I need to tie an ostringstream
to an istringstream so I can write to one and read back
what I wrote from the other.
I think you misunderstand what tieing means. All that means is that
tied streams are flushed when operations are performed on the other
stream.

I had expected the code tolook like:

ostringstream* ostr_stdin = new ostringstream();
istringstream* istr_stdin = new istringstream();
Why not create them on the stack?

ostringstream ostr_stdin;
istringstream istr_stdin;
I have also
tried variants like:

istr_stdin->str(ostr_stdin->str());
That just sets the two streams to have the same string value, not to
share a string.

or something similar with rdbuf(), which the documentation
(so far as such is available), suggests should work.
It should if you do it right.

Withthe deprecated classes istrstream and ostrstream this was
all no problem, you simply give the same char* to the constructor.
I also tried using the i/ostringstream constructors that take
a string as argument and giving the same string, or ditto
with char*, all to no avail.

Anyone know how to do this?


Use a stringstream:

#include <sstream>
#include <iostream>
using namespace std;

int main()
{
stringstream ss;

ostream& ostr_stdin = ss;
istream& istr_stdin = ss;

ostr_stdin.write("www", 3);
char buf[20];
buf[0] = '\0';
istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;
}

or share an rdbuf between two different streams:

#include <sstream>
#include <iostream>
using namespace std;

int main()
{
ostringstream ostr_stdin(ios::in | ios::out);
istream istr_stdin(ostr_stdin.rdbuf());

ostr_stdin.write("www", 3);
char buf[20];
buf[0] = '\0';
istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;
}

Or even create the stringbuf separately, and construct an istream and
ostream holding it.

Tom
Jul 19 '05 #3

P: n/a
Hi Rolf,

and thanks for the answer.

Your suggestion to use a single stringstream object works very
well in my situation and I will do that, thanks!

I realised in the meantime that tie() and str() have other
purposes - I am still a little puzzled as to why something like:

ostr_stdin->rdbuf(istr_stdin->rdbuf());

does not result in the tight coupling of the strings that I expected.
Maybe a bug in the stringstream class? Or have I misunderstood
this interface?

David
Rolf Magnus <ra******@t-online.de> wrote in message news:<bo*************@news.t-online.com>...
David Johnstone wrote:
Hi Group,

I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2).

I have a situation where I need to tie an ostringstream
to an istringstream so I can write to one and read back
what I wrote from the other.


That's not what tying does. If you tie an input stream and an output
stream together, the output stream will be flushed when you read
something from the input stream.
I had expected the code to
look like:

ostringstream* ostr_stdin = new ostringstream();
istringstream* istr_stdin = new istringstream();

istr_stdin->tie(ostr_stdin);

// this to test only...
// ====================

ostr_stdin->write("www", 3);
char buf[20];
buf[0] = '\0';
*istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;

but that just plain and simple doesn't work! I have also
tried variants like:

istr_stdin->str(ostr_stdin->str());


This will copy the string from ostr_stdin to istr_stdin, so they will
contain the same text afterwards, but of course if one changes its
text, the other one doesn't.
or something similar with rdbuf(), which the documentation
(so far as such is available), suggests should work. With
the deprecated classes istrstream and ostrstream this was
all no problem, you simply give the same char* to the constructor.
I also tried using the i/ostringstream constructors that take
a string as argument and giving the same string, or ditto
with char*, all to no avail.


Again, both will get a copy of that string. So if one changes its
string, the other string doesn't change.
Why don't you just use one stringstream? You can use that to read what
you wrote to it:

#include <sstream>
#include <iostream>

int main()
{
std::stringstream str;
std::string something;

str << "Hello";
str >> something;

std::cout << something << '\n';
}

Jul 19 '05 #4

P: n/a
tom_usenet <to********@hotmail.com> wrote in message news:<d9********************************@4ax.com>. ..
On 13 Nov 2003 01:12:54 -0800, da*******@mail.com (David Johnstone)
wrote:

...

#include <sstream>
#include <iostream>
using namespace std;

int main()
{
ostringstream ostr_stdin(ios::in | ios::out);
istream istr_stdin(ostr_stdin.rdbuf());

ostr_stdin.write("www", 3);
char buf[20];
buf[0] = '\0';
istr_stdin >> buf;
cout << "read word: \"" << buf << "\"" << endl;
}

Or even create the stringbuf separately, and construct an istream and
ostream holding it.

Tom


thanks Tom, for the most useful info. The stringstream solution
seems the best for me. Probably my rdbuf version didn't work because
I used the default ios:: mode which I assume is ios::out instead
of ios::in || ios::out when constructing the ostringstream. That
makes sense, I'll check it out tomorrow at work. I understand all
these classes better now,
thanks again,
David
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.