I have some 6k pictures in binary format (uint8), where I process the picture in blocks of 30 or so lines at time. The implication is that I end up having to skip through most of the file until I to get to the position I need to. So far the fastest thing seems to be to read the unnecessary part into a temporary variable and delete the variable afterwards, however logically this doesn’t seem like the most efficient way.
ifstream img;
int startingPixel = N;
for(int frame=0;frame<numFrames;frame++){
img.open("myfileAsFunctionOfFrame.bin",ios::in | ios::binary);
img.read((char*) &tempArray[0], startingPixel*sizeof(uint8));
img.read((char*) &myArray[frame*pixelsToRead], pixelsToRead*sizeof(uint8));
img.close();
}
delete [] tempArray;
I tried using img.ignore(), but that made it slower.
Any suggestions?
Use the seekg method. It was made for exactly what you’re trying to do.
http://www.cplusplus.com/reference/iostream/istream/seekg/
Hope it helps