stringstream tie problem

D

David Johnstone

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
 
R

Rolf Magnus

David said:
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';
}
 
T

tom_usenet

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 to
look 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.

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?

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::eek:ut);
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
 
D

David Johnstone

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 said:
David said:
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';
}
 
D

David Johnstone

tom_usenet said:
On 13 Nov 2003 01:12:54 -0800, (e-mail address removed) (David Johnstone)
wrote:

...

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

int main()
{
ostringstream ostr_stdin(ios::in | ios::eek:ut);
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::eek:ut instead
of ios::in || ios::eek:ut 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top