I have a binary file input.hgt
from the Shuttle Radar Topography Mission. It contains a 3601✕3601 matrix, stored as 2-Byte big-endian integer numbers. My code converts it to a readable text file output.csv
.
How can I make it better?
Here is my code:
#include <iostream>
#include <fstream>
using namespace std;
const int SRTM_SIZE = 3601;
static int height[SRTM_SIZE][SRTM_SIZE] = {{0},{0}};
int main() {
ifstream file("/storage/emulated/0/input.hgt", ios::in | ios::binary);
if(!file) {
cout << "Error opening file!" << endl;
return -1;
}
unsigned char buffer[2];
for (int i = 0; i < SRTM_SIZE; ++i) {
for (int j = 0; j < SRTM_SIZE; ++j) {
if(!file.read(reinterpret_cast<char*>(buffer), sizeof(buffer) )) {
cout << "Error reading file!" << endl;
return -1; }
height[i][j] = (buffer[0] << 8) | buffer[1];
} }
ofstream meinFile;
meinFile.open ("/storage/emulated/0/output.csv");
for(int x = 0; x < SRTM_SIZE; x++)
{ // from row 1 to row SRTM size
for(int y = 0; y < SRTM_SIZE; y++)
{// from column 1 to SRTM_Size
meinFile << height[x][y] << ",";
}
meinFile << endl;
}
meinFile.close();
cout<< "Gratulations!" <<endl;
cout<< "Your file has been converted sucessfully" <<endl;
return 0;
}