Fast read of certain bytes of multiple files in C/C++
- by Alejandro Cámara
I've been searching in the web about this question and although there are many similar questions about read/write in C/C++, I haven't found about this specific task.
I want to be able to read from multiple files (256x256 files) only sizeof(double) bytes located in a certain position of each file. Right now my solution is, for each file:
Open the file (read, binary mode):
fstream fTest("current_file", ios_base::out | ios_base::binary);
Seek the position I want to read:
fTest.seekg(position*sizeof(test_value), ios_base::beg);
Read the bytes:
fTest.read((char *) &(output[i][j]), sizeof(test_value));
And close the file:
fTest.close();
This takes about 350 ms to run inside a for{ for {} } structure with 256x256 iterations (one for each file).
Q: Do you think there is a better way to implement this operation? How would you do it?