读取4字节浮点矩​​阵文件并将其输出为.csv [英] Read 4 bytes floating point matrix file and output it as .csv

查看:64
本文介绍了读取4字节浮点矩​​阵文件并将其输出为.csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制文件,其矩阵3601 * 3601存储为2字节整数,我找到了一个代码(在下面发布),将其转换为可读的文本文件,如.csv



我的问题:我现在有一个矩阵3601 * 3601存储在一个二进制文件中4字节浮点,如何编辑我的代码来读取这个文件,然后输出它作为十进制存储在一个可读的文本文件中.csv如上所述?



这是2 Byte Int二进制文件的工作代码:



我尝试过:



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:

#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;
    }

推荐答案

将它们读入一个变量,就像你可以使用 short int一样 s:

Read them into a variable like you could have already done with the short ints:
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:

// 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;



但请注意,稍后从文本文件中读取并转换回浮点值的值可能稍有不同。这尤其适用于不使用 float 类型的完整精度打印时。




另请注意,您的代码会在CSV文件的最后一列之后生成一个额外的逗号。改为使用类似的东西:


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.


Note also that your code produces an additional comma after the last column in the CSV file. Use something like this instead:

if (y)
    meinFile << ",";
meinFile << height[x][y];
// When using the data buffer from my 2nd example:
//meinFile << data[x * SRTM_SIZE + y];

[/ EDIT]


这篇关于读取4字节浮点矩​​阵文件并将其输出为.csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆