How to move the pointer of istream by n characters

2020-07-16 08:58发布

问题:

I would like to skip a few characters in a binary file between two istream getlines. What is the best way to do it?

Shell I just read into a dummy variable with istream::read?

Or shell I use n = istream::tellg and istream::seekg = n + 1000?

回答1:

You can simply move a stream position relative to the current position by using the std::ios::cur position argument:

std::ifstream f("myfile.txt");   // current position 0
f.seekg(200, std::ios::cur);     // relative seek

Negative values are also permitted. See e.g. here.