Click here to Skip to main content
15,920,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a binary file that has a matrix 3601*3601 stored as 2 Byte Integer numbers, I found a code (posted below) that convert it to a readable text file such .csv

My question: I have now a matrix 3601*3601 stored in a binary file 4 Byte floating point, how to edit my code to read this file, then output it as decimal stored in a readable text file such .csv as above?

Here is the working code for 2 Byte Int binary:

What I have tried:

C++
#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;
    }
Posted
Updated 18-Jul-18 20:31pm
v2

1 solution

Read them into a variable like you could have already done with the short ints:
C++
float val;
file.read(reinterpret_cast<char*>(&val), sizeof(val));
// Write val to output text file here
You might even read them all at once into a buffer:
C++
// Use the heap here instead of the stack for large data
float *data = new float[SRTM_SIZE * SRTM_SIZE];
file.read(reinterpret_cast<char*>(data), sizeof(float) * SRTM_SIZE * SRTM_SIZE);
// Write values from array to output text file here
delete[] data;

But note that the values when read from the text file later and converted back to floating point might be slightly different. That applies especially when not printing with the full precision of the float type.

[EDIT]
Note also that your code produces an additional comma after the last column in the CSV file. Use something like this instead:
C++
if (y)
    meinFile << ",";
meinFile << height[x][y];
// When using the data buffer from my 2nd example:
//meinFile << data[x * SRTM_SIZE + y];
[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900