Can I open an ifstream (or set an existing one in any way) to only read part of a file? For example, I would like to have my ifstream read a file from byte 10 to 50. Seeking to position 0 would be position 10 in reality, reading past position 40 (50 in reality) would resualt in an EOF, etc. Is this possible in any way?
Can I open an ifstream (or set an existing one in any way) to
Share
It definetly can be done by implementing a filtering stream buffer: you would derive from
std::streambufand take the range you want to expose and the underlying stream buffer (well, a pointer to it) as arguments. Then you would seek to the start location. An overriddenunderflow()function would read from the underlying stream buffer into its buffer until has consumed as many characters as were desired. Here is a somewhat rough and entirely untested version:You can use a pointer to an instance of this stream buffer to initialuze an
std::istream. If this a recurring need, you might want to create a class derived fromstd::istreamsetting up the stream buffer instead.