标签:进制 大小 number 结果 span read str delete param
#include <iostream> #include <fstream> #include<vector> using namespace std; /** * @brief 读取二进制文件中的数据 * @param data_fname 二进制文件名 * @param rows * @param lines * @param depths * @param raw_data_matrix 输出结果 * * @return 返回说明 * -<em>-1</em> fail * -<em>0</em> succeed */ int ReadDATA_SV(const char*data_fname/*in*/, int rows/*in*/, int lines/*in*/, int depths/*in*/, float *raw_data_matrix/*out*/); int main() { //参数设置 const int rows = 249; const int lines = 205; const int depths = 35; const char *data_fname = "S2020031120_000.DAT"; float*raw_data_matrix = new float[rows*lines*depths]; //函数调用 int ret=ReadDATA_SV(data_fname, rows, lines, depths, raw_data_matrix); if (ret != 0){ printf("函数调用失败!\n"); return ret; } //告诉你这个一维数组怎么用,比如输出(:,:,1) cout << endl << "一维数组输出结果" << endl; for (int i = 0; i < 2; ++i) { for (int j = 0; j <lines; ++j) { for (int k = 0; k < 1; ++k) { cout << raw_data_matrix[i*lines*depths + j*depths + k] << " "; } } cout << endl; } //如果要转成Vector直接转 vector<vector<vector<float>>>raw_data_matrix_vector(rows, vector<vector <float>>(lines, vector < float>(depths)));; for (int i = 0; i < rows; ++i) { for (int j = 0; j <lines; ++j) { for (int k = 0; k < depths; ++k) { raw_data_matrix_vector[i][j][k] = raw_data_matrix[i*lines*depths + j*depths + k]; } } } //输出测试 cout <<endl<< "vector输出结果" << endl; for (int i = 0; i < 2; ++i) { for (int j = 0; j <lines; ++j) { for (int k = 0; k < 1; ++k) { cout << raw_data_matrix_vector[i][j][k] << " "; } } cout << endl; } delete[]raw_data_matrix; raw_data_matrix = NULL; system("pause"); return 0; } int ReadDATA_SV(const char*data_fname/*in*/, int rows/*in*/, int lines/*in*/, int depths/*in*/, float *raw_data_matrix/*out*/) { int dataSumNumber = rows*lines*depths; float *rowData = new float[dataSumNumber]; ifstream finFile(data_fname, ios::in | ios::binary); if (!finFile){ cout << "error" << endl; return -1; } //先跳过8个字节 finFile.seekg(8, ios::beg); finFile.read((char*)rowData, dataSumNumber * sizeof (float)); int readedBytes1 = finFile.gcount(); //看刚才读了多少字节 cout << "readedBytes1:" << readedBytes1 << endl; //读出来 rowData 是个(7815,205)的二维数组,还要转为三维的 raw_data_matrix(249,205,35) for (int dep = 0; dep < depths; ++dep){ int ind_i = dep*rows; for (int row = 0; row < rows; ++row){ for (int lin = 0; lin < lines; ++lin){ raw_data_matrix[row*lines*depths + lin*depths + dep] = rowData[(ind_i + row)*lines + lin]; } } } //释放空间 delete[]rowData; rowData = NULL; return 0; }
C++读取特定大小二进制文件,一维数组形式和vector形式
标签:进制 大小 number 结果 span read str delete param
原文地址:https://www.cnblogs.com/rswss/p/13212821.html