Convert binary raster file to text CSV 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 &lt;iostream&gt;
#include &lt;fstream&gt;
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 &lt;&lt; "Error opening file!" &lt;&lt; endl;
return -1;
}

unsigned char buffer[2];


for (int i = 0; i &lt; SRTM_SIZE; ++i) {

for (int j = 0; j &lt; SRTM_SIZE; ++j) {

if(!file.read(reinterpret_cast&lt;char*&gt;(buffer), sizeof(buffer) )) {

    cout &lt;&lt; "Error reading file!" &lt;&lt; endl;

return -1; }

height[i][j] = (buffer[0] &lt;&lt; 8) | buffer[1]; 

} }


ofstream meinFile;

meinFile.open ("/storage/emulated/0/output.csv");

    for(int x = 0; x &lt; SRTM_SIZE; x++)
    { // from row 1 to row SRTM size

        for(int y = 0; y &lt; SRTM_SIZE; y++)
        {// from column 1 to SRTM_Size

        meinFile &lt;&lt; height[x][y] &lt;&lt; ",";

        }

        meinFile &lt;&lt; endl;
    }

            meinFile.close();
    cout&lt;&lt; "Gratulations!" &lt;&lt;endl;
    cout&lt;&lt; "Your file has been converted sucessfully" &lt;&lt;endl;
    return 0;
}
Avoid using namespace std.
Assuming sizeof(int) is 4, the height array takes more than 40 MB of space. I understand that by modern standards it is a small change, but still a good citizen should not request more than is really needed.

I recommend a stream-like processing: read 2 bytes, convert them, and print out. This way you only need 2 bytes of storage.
The program terminates each row with a dangling comma. I don't know what the RFC says about it. Consider however

    for(int x = 0; x &lt; SRTM_SIZE; x++) {
        for(int y = 1; y &lt; SRTM_SIZE - 1; y++) {
            meinFile &lt;&lt; height[x][y] &lt;&lt; ",";
        }
        meinFile &lt;&lt; height[x][SRTM_SIZE - 1] &lt;&lt; std::endl;
    }

You test reading errors. Writing may also fail.
Error detection is important. Error reporting is equally important.

Can't open file - why? Is it insufficient permission? file not found? corrupted media?

Can't read file - why? Can't write file - why?

Check out strerror, perror or similar facilities.