C ++使用内存中的ifstream [英] c++ use ifstream from memory

查看:149
本文介绍了C ++使用内存中的ifstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码使用ifstream从文件中读取一些数据,一切正常.

I have some code that uses ifstream to read some data from a file and everything works.

现在我希望不修改某些代码,而是从内存中读取此数据,实际上我有一个包含数据的char * ...

Now I wish, without modifying some code, read this data from a memory, actually I have a char * that contains the data...

如何在不有效读取文件的情况下将char *数据放入ifstream?

How can I put my char * data into a ifstream without reading effectively the file?

推荐答案

如果可以稍微更改使用ifstream&的代码以使用istream&,则可以轻松地在ifstream istringstream (用于从内存中读取数据):

If the code that uses the ifstream& could be changed slightly to use an istream& then you could easily switch between ifstream and istringstream (for reading data from memory):

void read_data(std::istream& in)
{
}

来电者:

std::istringstream in_stream(std::string("hello"));
read_data(in_stream);

std::ifstream in_file("file.txt");
read_data(in_file);

这篇关于C ++使用内存中的ifstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆