将二进制数据 (.tec) 读入 C++ 程序变量矩阵

问题描述

所以我期望数据文件通常以 .tec 结尾,但也被视为 .dat,其 ASCII 版本如下所示:

Title="UPSCALE DATA"
Variables="X","Y","Z","U","V","W","rho"

Zone I=  3,J=  3,K=  3,U=POINT,V=POINT,W=POINT,rho=POINT
         0         0         0              1              2              3              4
         0         0         1              1              2              3              4
         0         0         2              1              2              3              4
         0         1         0              1              2              3              4
         0         1         1              1              2              3              4

这里的前 3 列和“I=”、“J=”和“K=”后面的数字在程序中被读为整数。其他列将在 C++ 中作为浮点数据类型读取。

这是我用于如何使用以下函数以 ASCII 格式编写上述文件代码

#ifndef Tecplotter_H
#define Tecplotter_H

#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <iomanip>

static void Tecplot(int counter,std::string prefix,float* xvel,float* yvel,float* zvel,float* rho,int xRes,int yRes,int zRes) {
    
    char buffer[80];
    sprintf(buffer,"%04i",counter);
    std::string number = std::string(buffer);
    std::string filenameTec = prefix + number + std::string(".tec");
    printf("Writing 3D Tec file %s\n",filenameTec.c_str());

    std::cout << "Printing to Tecplot files" << std::endl;

    std::ofstream myfile;
    myfile.open(filenameTec.c_str(),std::ofstream::trunc);

    myfile << "Title=\"UPSCALE DATA\"" << '\n';
    myfile << "Variables=\"X\"," << "\"Y\"," << "\"Z\",";
    myfile << "\"U\"," << "\"V\"," << "\"W\",";
    myfile << "\"rho\""; 
    myfile << "\n\n";

    myfile << "Zone I=  " << xRes << ",J=  " << yRes << ",K=  " << zRes << ",";
    myfile << "U=POINT," << "V=POINT," << "W=POINT," << "rho=POINT" << '\n';


    // register matrices in style X,Y,Z,U,V,W,rho
    int index = 0;
    for (int i = 0; i < xRes; i++)
        for (int j = 0; j < yRes; j++)
            for (int k = 0; k < zRes; k++)
            {
                myfile << std::setw(10) << i << std::setw(10) << j << std::setw(10) << k;
                myfile << std::setw(15) << xvel[index] << std::setw(15) << yvel[index] << std::setw(15) << zvel[index] << std::setw(15) << rho[index];
                myfile << '\n';
                index++;
            }

    myfile.close();

}
#endif

我的问题有两个:

  1. 我将如何重写上述打印代码以执行相同的操作,但以二进制形式执行(不破坏代码,因为我尝试将“| ios::binary”标记添加到 ofstream 中,但没有成功?
  2. 立>
  3. 然后什么代码会将该代码读回到程序中并放入相同的数组变量中?

本质上,我想创建一个中间人,将数据打印成二进制,然后从二进制读回程序。

免责声明:我是一名航空工程师,而不是软件工程师,所以对待我就像第一年一样。其次,我使用的更广泛的代码(不包括在内)不是我自己的,而是在前一段时间写的,所以如果数据类型或某些东西看起来过时了,我真的没有能力将其他人的代码重写为 2021 年的效率。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)