I'm trying to refactor some code/libraries by trying to embed some resource files as binary objects using the objcopy tool. The files are .txt
and .xml
.
The original library contains some file read/write code, based on fopen
, which I replaced by fmemopen()
in order to load the files from memory instead of getting them from the disk.
My issue is that, the library also contains some std::ifstream
objects that are used to read some other files from disk, and my question is how can I read those too from the memory?
Ie I need either to assign a FILE* to an ifstream somehow, or if there is an equivalent of fmemopen()
that is used for ifstreams.
std::ifstream input(pathToFilters);
NB. Please note that I'm trying to get an ifstream FROM a FILE and not the inverse (which has been already answered)
Thanks!
Rather than open a FILE *
, then try to create a stream from that, it's almost certainly going to be easier to create an istream
that reads directly from memory.
An istringstream
reads from a memory buffer, so that part is pretty easy.
So, you can create an istringstream
object, passing it the data you want to read from, and then pass the istringstream
to the code that expects to read from a stream. Normally, most code that deals with a stream should accept a reference to an istream
or an ostream
, in which case it'll accept a stringstream
just as well as an fstream
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.