473,382 Members | 1,692 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,382 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 5757
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bill Beacom | last post by:
Hi all, I am trying to use stringstream to assemble a text message from a set of user-defined objects that have the insertion operator overloaded. It seems to be putting them into the...
3
by: Mike Chirico | last post by:
Sorry about the newbie question. What's the best way to convert numbers to strings in C++. The following works, but is it better than using the sprintf() "old C" way of converting? #include...
5
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ;...
3
by: David Olsson | last post by:
Hello! I have a little problem with one of the get methods of the stringstream class. Consider the code: #include <iostream> #include <sstream> #include <string> using namespace std;
2
by: avidamani | last post by:
Hi, I have a c++ application compiled using Compaq C++ V6.5-014 for Compaq Tru64 UNIX V5.1A (Rev. 1885) Compiler Driver V6.5-014 (cxx) cxx Driver The problem is that the application is...
10
by: vigacmoe | last post by:
I was trying to cast some strings to integers with stringstream, then this strange problem poped up. Here is my test code: stringstream conv; string from; int to; from = "1"; conv << from;
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
0
by: ziyanjoe | last post by:
Hi! I am writing a program for the robot that runs as a daemon on a linux machine. Since debug output cannot be seen on stdout, I want to create an iostream to handle all the output messages....
4
by: mthread | last post by:
Hi, I am using a string variable in which I do lot of appending. The only difficulty I am facing as of now is appending a integer/float value to this variable. Although I can accomplish this task...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.