Search code examples
c++ifstreamstringstream

C++ Convert stringstream into ifstream?


I am working with two pieces of code,

The first class uses an AAssetManager (Android NDK object) to read an "asset" and save it's contents to a single string. It then turns the string into a stringstream and parses it.

The second class parses type ifstream, which must be associated with a file.

Is there some way I can convert a string or stringstream into an ifstream so I don't have to change the entirety of one of these classes?


Solution

  • You can turn the ifstream into an istream. An ifstream is simply a more specific form of an istream. A stringstream inherits from an istream, so you can just pass the stringstream directly. If this is not available to you, then you would store the string to a file, then reopen the file with an ifstream, and pass that ifstream to the second function.