473,387 Members | 1,834 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.

cin to stringstream

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 ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)
Jul 22 '05 #1
5 10812

"cherico" <ch*****@bonbon.net> wrote in message
news:af**************************@posting.google.c om...
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 ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)


Does this count?

void copy_string(istream& in, ostream& out)
{
string str;
in >> str;
out << str;
}

stringstream ss;
copy_string(cin, ss);

Its still uses a intermediate string but its hidden from the rest of your
code.

Other than that I don't think there is a way. Why is it an issue? There
maybe another way to solve whatever problem you are having.

john
Jul 22 '05 #2
Overload operator >>, it solves.[B

istream& operator >> (istream& in, stringstream& ss);
int main (int argc, char* argv[])
{
stringstream ss; cin >> ss; cout << ss.str() << endl; return 0;
}
istream& operator >> (istream& in, stringstream& ss){
string s; in >> s; ss << s; return in;
}

On Tue, 7 Jun 2004, cherico wrote:
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 ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)


-- xiaobin
Jul 22 '05 #3
On 7 Jun 2004 23:11:47 -0700, ch*****@bonbon.net (cherico) wrote:
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 ;
string str ;
cin >> str ;
ss << str ;

Is there any way to put data from cin into stringstream
directly?

such as :
stringstream ss ;
cin >> ss ;

(which doesn't work...)


If you don't mind putting all of the data into the stringstream, you
can do:

ss << cin.rdbuf();

Alternatively, there are plenty of ways of copying data from one to
the other using algorithms. e.g.

std::istreambuf_iterator<char> begin(cin), end;
std::ostreambuf_iterator<char> out(ss);
std::copy(begin, end, out);
//or use a different algo, like copy_n.

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
ch*****@bonbon.net (cherico) wrote:
I'd like to read stings from cin and put them
in stringstream.
If you read strings and put them into a stringstream you are implicitly
removing whitespace: is this by design or by accident?
stringstream ss ;
cin >> ss ;


Here are a few alternatives which do not remove whitespace:

std::stringstream ss;
ss << std::cin.rdbuf();

std::copy(std::istreambuf_iterator<char>(std::cin) ,
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(ss));

.... and a few alternatives which do remove whitespace:

std::copy(std::istream_iterator<std::string>(std:: cin),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(ss));

bool my_isspace(char c) { return std::isspace(c); } // in namespace scope
std::remove_copy_if(std::istreambuf_iterator<char> (std::cin),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(ss), my_isspace);
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #5
On 8 Jun 2004 06:06:20 -0700, di***********@yahoo.com (Dietmar Kuehl)
wrote:
bool my_isspace(char c) { return std::isspace(c); } // in namespace scope


There's a missing conversion in that isspace call:

bool my_isspace(char c) {
return std::isspace(std::char_traits<char>::to_int_type(c ));
}

otherwise you get UB for negative values of c (other than EOF).

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #6

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...
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
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...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
9
by: martinezfive | last post by:
Hi, I feel no doubt some documentation contains my answer, so bare with me. Given the following: #inclde <stdio.h> #include <sstream> void f() { std::stringstream a("Hello World!\n");
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
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...
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: 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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.