cin to stringstream

Discussion in 'C++' started by cherico, Jun 8, 2004.

  1. cherico

    cherico Guest

    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...)
     
    cherico, Jun 8, 2004
    #1
    1. Advertising

  2. "cherico" <> wrote in message
    news:...
    > 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
     
    John Harrison, Jun 8, 2004
    #2
    1. Advertising

  3. cherico

    Xiaobin Yang Guest

    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
     
    Xiaobin Yang, Jun 8, 2004
    #3
  4. cherico

    tom_usenet Guest

    On 7 Jun 2004 23:11:47 -0700, (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::eek:streambuf_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
     
    tom_usenet, Jun 8, 2004
    #4
  5. (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::eek:streambuf_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::eek:stream_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::eek:streambuf_iterator<char>(ss), my_isspace);
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.contendix.com> - Software Development & Consulting
     
    Dietmar Kuehl, Jun 8, 2004
    #5
  6. cherico

    tom_usenet Guest

    On 8 Jun 2004 06:06:20 -0700, (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
     
    tom_usenet, Jun 8, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens

    stringstream

    Marc Schellens, Jul 1, 2003, in forum: C++
    Replies:
    3
    Views:
    537
  2. TaiwanNoWhere

    cin and cin.clear() problem

    TaiwanNoWhere, Oct 13, 2003, in forum: C++
    Replies:
    8
    Views:
    4,109
    P.J. Plauger
    Oct 17, 2003
  3. Chris Mantoulidis

    std::cin.ignore() and std::cin.clear()

    Chris Mantoulidis, Jan 6, 2004, in forum: C++
    Replies:
    5
    Views:
    17,183
    Kevin Saff
    Jan 6, 2004
  4. Aleander

    cin and cin.getline()

    Aleander, Mar 6, 2005, in forum: C++
    Replies:
    5
    Views:
    8,714
    Alex Vinokur
    Mar 6, 2005
  5. Fernando
    Replies:
    4
    Views:
    1,694
    Fernando
    Nov 16, 2011
Loading...

Share This Page