简体   繁体   English

C++ 将 stringstream 转换为 ifstream?

[英]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.第一个类使用 AAssetManager(Android NDK 对象)读取“资产”并将其内容保存为单个字符串。 It then turns the string into a stringstream and parses it.然后它将字符串转换为字符串流并对其进行解析。

The second class parses type ifstream, which must be associated with a file.第二个类解析类型 ifstream,它必须与文件相关联。

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?有什么方法可以将字符串或字符串流转换为 ifstream,这样我就不必更改其中一个类的全部内容吗?

You can turn the ifstream into an istream .您可以将ifstream转换为istream An ifstream is simply a more specific form of an istream . ifstream只是istream一种更具体的形式。 A stringstream inherits from an istream , so you can just pass the stringstream directly. stringstreamistream继承,因此您可以直接传递stringstream 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.如果这对您不可用,那么您可以将该字符串存储到一个文件中,然后使用ifstream重新打开该文件,并将该ifstream传递给第二个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM