472,126 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

stringstream tie problem

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
4 5644
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
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
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
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.

Similar topics

2 posts views Thread by Bill Beacom | last post: by
3 posts views Thread by Mike Chirico | last post: by
5 posts views Thread by cherico | last post: by
3 posts views Thread by David Olsson | last post: by
2 posts views Thread by akitoto | last post: by
reply views Thread by leo001 | last post: by

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.