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
You could read the bytes that you want into a string or char array, then you can use that string with a istringstream, and use that instead of your ifstream. Example:
std::ifstream fin("foo.txt");
fin.seekg(10);
char buffer[41];
fin.read(buffer, 40);
buffer[40] = 0;
std::istringstream iss(buffer);
for (std::string s; iss >> s; ) std::cout << s << '\n';
If you need to handle binary files, you can do that too:
std::ifstream fin("foo.bin", std::ios::binary | std::ios::in);
fin.seekg(10);
char buffer[40];
fin.read(buffer, 40);
std::istringstream(std::string(buffer, buffer+40));