Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to read in objects with a given offset?

Now I have a file with many data in it. And I know the data I need begins at position (long)x and has a given size sizeof(y) How can I get this data?

like image 530
shevron Avatar asked Jun 26 '09 13:06

shevron


2 Answers

Use the seek method:

ifstream strm;
strm.open ( ... );
strm.seekg (x);
strm.read (buffer, y);
like image 107
eduffy Avatar answered Sep 20 '22 08:09

eduffy


You should use fseek() to change your "current position" in the file to the desired offset. So, if "f" is your FILE* variable and offset is the offset this is how the call should look like (modulo my leaky memory):

fseek(f, offset, SEEK_SET);
like image 20
Itay Maman Avatar answered Sep 21 '22 08:09

Itay Maman