Reading binary data (.tec) into C++ program variable matrices

55 Views Asked by At

So I am expected a data file usually with ending .tec but also seen as .dat with an ASCII version that looks like such:

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

Here the first 3 columns and the numbers following "I=", "J=" and "K=" are to be read as integers in the program. And the other columns are to be read as float data types in C++.

This is the code I used on how to write the above file in ASCII with the following function:

#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

My question is two fold:

  1. How would I rewrite the above printing code to do the same thing but in binary (without the code breaking, as I tried adding an '| ios::binary" tag to the ofstream and that didn't work?
  2. What code would then read that code back into the program into the same array variables?

Essentially I want to create a middle man where the data gets printing into binary and then read back into the program from binary.

Disclaimer: I am an Aeronautical Engineer not a Software engineer so treat me like a first year. Secondly the broader code I am using (not included) is not my own and was written a while back so if the data types or something seem outdated I am not really in the capacity to rewrite someone else's code into the 2021 efficiency.

0

There are 0 best solutions below