Any way to create an ifstream within an ifstream?

Hi! I've been tinkering around with .GRP files from back in the day. GRP files store content in them with a set size defined in the GRP file. Essentially just uncompressed files inside of the .GRP file.

Right now my solution to reading the individual files is to copy it directly into memory. Which with modern computers with those small files isn't such a big deal, but I would like to avoid it.

Essentially I'm using an ifstream to read the GRP file, then copying files from the ifstream into memory. Is there any way to create a separate ifstream or something like it that's scoped between the beginning and end positions of the file I want to read that's inside the GRP file?

I know I could simply dump the file and read it that way, but that's not what I want to do.

I could also create a wrapper class that kind of hacks that functionality, but if there's a pre-existing way to do it in C++ I'd like to know.

I just want to find a more efficient way to read it than copying data, either to a new file or memory.

Third party libs are kind of a last resort for me, so I would also like to avoid the "use XYZ lib" answer if at all possible.
Read whichever record(s) you're using into stringstreams. Treat the stringstreams as you would treat file streams. This does involve copying the data multiple times, however.

I just want to find a more efficient way to read it than copying data, either to a new file or memory.

There is no way to read a file without copying the data to memory.
Last edited on
There is no way to read a file without copying the data to memory.


That makes no sense, or it is a bit misleading. Of course there's a way. There's no way you could load say a 4 GB file into memory on a system with 2 GB of RAM. Those files have to be streamed.

If you wanted to copy a very large file like that, it would make absolutely no sense for the entire file to be copied into memory.

I know game engines like Source create virtual filesystems (which Source uses .vpks), and has its own handling for reading those files. If that's what I have to do, that's what I would like to know.
I keep hearing about mmap, though I never get around to looking deeper into how to use it; Memory mapping files might do what you're thinking of. I'm not sure how cross-platform this option is.

http://stackoverflow.com/questions/45972/mmap-vs-reading-blocks
Last edited on
Topic archived. No new replies allowed.