I'm trying to translate a matlab code which reads an hgt file to c++ and I've come about this resource. However, instead of reading the file by column and row I was wondering if there were any way to use fread to get the desired values faster. The matlab code looks like this:
% ilat and ilon, both vectors, already calculated
% Calculate the offset to each point
offset = ilon*nbytes_per_lon + ilat*2;
% Read the database
path_data = zeros(size(path_lon));
for i = 1:length(path_lon);
status = fseek(fid, offset(i), 'bof');
path_data(i)=fread(fid,[1],'integer*2');
end
My c++ code looks like this:
for (i = 0; i < path_lon.size(); i++) {
offset[i] = ilon[i] * nbytes_per_lon + ilat[i] * 2;
fseek(pFile, offset[i], SEEK_SET); // SEEK_SET == bof
path_data[i] = fread(buffer, 2, 1, pFile);
}
The part I'm having trouble converting is the fread portion, on the c++ side it just gives me all 1s. I've read the matlab and c++ documentation for fread but can't seem to properly translate. Any help would be appreciated, thank you!
EDIT: I've gone away with the direct translation and instead am trying to used the linked resource to iterate through the hgt file and get the values that I want. Here's my test code:
std::ifstream::pos_type size = 2;
char* memblock = new char[size];
if(!file) {
std::cout << "Error opening file!" << std::endl;
return -1;
}
for (int i = 0; i < SRTM_SIZE; i++) {
for (int j = 0; j < SRTM_SIZE; j++) {
height[i][j] = (memblock[0] << 8) | memblock[1];
}
}
file.seekg(0, std::ios::beg);
file.read(memblock, size);
std::cout << "height at [5][5]: " << height[5][5] << std::endl;
std::cout << "height at [10][10]: " << height[10][10] << std::endl;
The problem I'm having is that both height[5][5] and height[10][10] are giving me the same values (0). What am I doing wrong?