简体   繁体   English

如何将ifstream转换为istream

[英]How can I convert an ifstream to an istream

Is there any way I can convert an ifstream to an istream ? 有什么方法可以将ifstream转换为istream吗? I have a function called getToken(istream* br) that accepts an istream as the parameter, but within my main() I use an ifstream to read a file, and I must use this same ifstream to call getToken . 我有一个名为getToken(istream* br)的函数,该函数接受istream作为参数,但是在我的main()我使用ifstream读取文件,并且必须使用相同的ifstream调用getToken How can I overcome this issue? 我该如何克服这个问题?

As mentioned in the comments, you don't need to do anything fancy. 如评论中所述,您无需做任何花哨的事情。 The std::ifstream type inherits from std::istream , so any function that takes an istream* can take a pointer to an ifstream as input. std::ifstream类型继承自std::istream ,因此任何采用istream*函数都可以将指向ifstream的指针作为输入。 For example: 例如:

string getToken(istream* stream) {
    ...
}

ifstream fileStream;
getToken(&fileStream);

The streams classes were specifically designed to make operations like this one possible. 流类经过专门设计,以使这种操作成为可能。

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

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