CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    [RESOLVED] string as stream

    Hello!

    I'm solving one problem related to parsing of text files. It would be very convenient for me to read ifstream line by line at first, treat every line as string (preferably c++ string), then convert that string to istream and read from the istream.

    So:

    Is there an easy way to convert std::string into std::istream?

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: string as stream

    Quote Originally Posted by andrey_zh View Post
    Hello!

    I'm solving one problem related to parsing of text files. It would be very convenient for me to read ifstream line by line at first, treat every line as string (preferably c++ string), then convert that string to istream and read from the istream.

    So:

    Is there an easy way to convert std::string into std::istream?
    Yes, an istringstream is a istream, so you can transform your string into an istringstream and get the same result. Said conversion is actually very easy:

    Code:
    std::istringstream iss(myString)
    Simple as that. Don't forget to include <sstream>. It is OK and easier to construct and destroy the iss on every iteration. You can choose to use a single iss that you recycle on every iteration, but it makes error handling and cleanup harder.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Mar 2009
    Location
    Riga, Latvia
    Posts
    128

    Re: string as stream

    It works great! Thanks! Thread resolved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured