1

因此,我预计数据文件通常以 .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. 然后什么代码会将该代码读回程序中相同的数组变量中?

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

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

4

0 回答 0