57 c++ 读取二进制文件: 以.raw后缀的TDF_Voxel_Size文件为例

0 引言

毕业设计中用到了.raw格式的文件,需要将该文件按照M*N*P的矩阵格式加载到内存中。采用 #include <fstream> 中的FILE* 加载,并针对数据占用字节数,以 float* f = (float*) buffer; 语句强制转换类型,得到规整的文本数据。最后,由于数据量超出了 vector.max_size(),采用三维vector对数据进行存储,完成加载目标。

1 输入输出

输入包含两个文件, 分别是:

  1、bicycle000002.size文件,包含数据格式描述({m, n, p, q});

  2、bicycle000002.raw,序列化存储矩阵中的每一个数。

期望的输出:

  将上述.raw文件中的数以矩阵 m*n*p的形式读入到内存中。

2 demo

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

void ReadSizeFile(const char *file_path, int size[4])
{
    FILE *file;
    file = fopen(file_path, "rt+");
    if (!file) //条件不成立,则说明文件打开失败
    {
        fclose(file);
        cout << "打开文件失败! " << endl;
    }
    fscanf(file, "Float32(%d, %d, %d, %d)", &size[0], &size[1], &size[2], &size[3]);
    fclose(file);
}
void ReadTDFfile(const char *file_path, vector<vector<vector<float>>>& object_TDF_data, int size[4]){
    FILE *pFile;
    long lSize;
    char *buffer;
    size_t result;
    pFile = fopen(file_path, "rb");
    if (pFile == NULL)
    {
        fputs("File error", stderr);
        exit(1);
    }
    // obtain file size:
    fseek(pFile, 0, SEEK_END);
    lSize = ftell(pFile);
    rewind(pFile);   // 指针倒带至头
    // allocate memory to contain the whole file:
    buffer = (char *)malloc(sizeof(char) * lSize);
    if (buffer == NULL)
    {
        fputs("Memory error", stderr);
        exit(2);
    }
    // copy the file into the buffer:
    result = fread(buffer, 1, lSize, pFile);
    if (result != lSize)
    {
        fputs("Reading error", stderr);
        exit(3);
    }

    float* f = (float*)buffer;
    /* the whole file is now loaded in the memory buffer. */
    for(int i=0; i< size[0]; ++ i){
        vector<vector<float>> column_row;
        for(int j=0; j< size[1]; ++ j){
            vector<float> row;
            for(int k=0; k<size[2]; ++ k){
               row.push_back(*f);
               ++ f;
            }
            column_row.push_back(row);
            row.clear();
        }
        object_TDF_data.push_back(column_row);       
        column_row.clear();
    }
    // terminate
    fclose(pFile);
    free(buffer);
}

int main()
{
    const char *file_path_size = "bicycle000002.size";
    int size[4];
    ReadSizeFile(file_path_size, size);

    vector<vector<vector<float>>> object_TDF_data;
    const char *file_path_data = "bicycle000002.raw";
    ReadTDFfile(file_path_data, object_TDF_data, size);
    return 0;
}

3 测试文件下载链接

我把相关project开源在了我的github上,欢迎下载。

https://github.com/2017Greg/myVscodeProject/tree/master

下载的时候请同时下载代码readTDF.cpp和测试文件 bicycle000002.size和bicycle000002.raw

 

原文链接: https://www.cnblogs.com/ghjnwk/p/10558730.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    57 c++ 读取二进制文件: 以.raw后缀的TDF_Voxel_Size文件为例

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/291779

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午1:55
下一篇 2023年2月15日 下午1:56

相关推荐