|
-
November 22nd, 2010, 05:49 AM
#1
[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?
-
November 22nd, 2010, 05:56 AM
#2
Re: string as stream
 Originally Posted by andrey_zh
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.
-
November 22nd, 2010, 08:14 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|