Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move the pointer of istream by n characters?

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?

like image 781
hyperknot Avatar asked Jun 19 '11 17:06

hyperknot


1 Answers

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.

like image 112
Kerrek SB Avatar answered Oct 06 '22 15:10

Kerrek SB