Re: How to convert std::string to std::istream?

Discussion in 'C++' started by Ian Collins, Nov 13, 2009.

  1. Ian Collins

    Ian Collins Guest

    Water Lin wrote:
    > I have a function that use std::istream as parameter. Right now I need
    > to pass a std::string into it, how can I convert std::string to
    > std::istream type?


    You can't directly, but you can use a stringstream:

    #include <iostream>
    #include <sstream>

    void f( std::istream& in )
    {
    std::string s;
    in >> s;
    std::cout << s << std::endl;
    }

    int main()
    {
    std::string s("hello");
    std::istringstream ss( s );

    f( ss );
    }

    --
    Ian Collins
    Ian Collins, Nov 13, 2009
    #1
    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. Gernot Frisch

    from std::string to std::istream?

    Gernot Frisch, Mar 17, 2005, in forum: C++
    Replies:
    4
    Views:
    14,287
    Victor Bazarov
    Mar 18, 2005
  2. Anne-Marte
    Replies:
    3
    Views:
    5,930
    Grant
    Mar 30, 2005
  3. Jason K
    Replies:
    6
    Views:
    3,986
    Jeff Flinn
    May 12, 2005
  4. Jeffrey Walton
    Replies:
    10
    Views:
    937
    Mathias Gaunard
    Nov 26, 2006
  5. xmllmx
    Replies:
    5
    Views:
    587
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page