创建具有在运行时生成的名称的结构,并在程序中引用它们

Create structs with names generated at runtime and refer to them within program

本文关键字:程序 引用 结构 运行时 创建      更新时间:2023-10-16

我在目录中存储了一组数据文件。例如

./FT/Fourier_1
./FT/Fourier_2
./FT/Fourier_3
...

我的代码最初生成了这些文件的路径列表。

std::string fileStringSearch="Fourier";
std::stringstream resultFileName;
std::vector<std::string> fileList;
int numLines=0;
DIR *parentDirPointer;
struct dirent *dp;
if ((parentDirPointer = opendir("FT")) == NULL)
{
    std::cout << "Unable to open the parent (FT) directory" << std::endl;
    return(3);
}
while ((dp = readdir(parentDirPointer)) != NULL)
{
    std::string testFileName = dp->d_name;
    if (testFileName.find(fileStringSearch) != std::string::npos)
    {
        resultFileName << "FT/" << dp->d_name;
        std::string blahblah=resultFileName.str();
        fileList.push_back(blahblah);
        numLines++;
        resultFileName.str(std::string());
        resultFileName.clear();
    }
};
sort(fileList.begin(),fileList.end());
for (unsigned n=0; n<fileList.size(); ++n)
{
    resultFileName << fileList.at(n) << std::endl;
}
FTFilePaths = resultFileName.str();

我想从每个文件中读取数据并以某种格式存储,以后可以阅读,在功能等中使用,等等

我目前的想法是一个结构 - 我有:

struct Wavenum_struct {
    std::string name;
    double timestep;
    double indexToK_Multiplier;
    std::vector<double> Amp_k;
    std::vector<double> dFTdt_prev_to_this;
    }

,在我程序的稍后时刻,我读了这些文件,例如:

for (int lineCounter=0; lineCounter < numLines; lineCounter++)
    {
        getline(readPath, FilePathLine);
        c = FilePathLine.c_str();
        readFile.open(c);
        extern Wavenum_struct c;
        c.name = FilePathLine;
        //print_name(c);
        while(getline (readFile, lineToRead))
        {
            readFile >> value;
            c.Amp_k.push_back(value);
        }
        //print_amps(c);
    }

注释的print_amps(c);可以通过一个功能正常工作:

void print_amps(struct Wavenum_struct t)
{
    for(int i=0; i<t.Amp_k.size(); i++)
    {   
        std::cout << i << ": " << t.Amp_k[i] << std::endl;
    }
}

但这显然打印了每个结构的振幅,只有一次。如果我想稍后在程序中引用特定的结构,并选择性地打印它们,(或不打印它们,但将它们用于某些功能),例如

void dFTdt(struct Wavenum_struct t_i, struct Wavenum_struct t_i1)
{
    int numWavenums = t_i.Amp_k.size();
    double dt = t_i1.timestep - t_i.timestep;
    double dFTdt[numWavenums];
    for (int k=0; k<numWavenums; k++)
    {
        dFTdt[k] = (t_i1.Amp_k[k] - t_i.Amp_k[k])/dt;
    }
    t_i1.dFTdt_prev_to_this.assign(dFTdt, dFTdt+numWavenums);
}

那么我似乎无法到达任何地方,因为C返回被识别为for循环外的con * char,而我尝试过的任何东西都喜欢:

print_amps(reinterpret_cast<Wavenum_struct*>("FT/Fourier_1"));

拒绝编译。

我认为我需要的可能涉及函数的指针和print_name()作为结构的函数,但这似乎并不可能对我的void dftdt()函数有所帮助,我仍然不知道如何指定结构一旦C不再给出该结构的名称。

这是任何可能的吗?

在此功能中(实际上应该具有名称):

for (int lineCounter=0; lineCounter < numLines; lineCounter++)
{
  getline(readPath, FilePathLine);
  c = FilePathLine.c_str();
  readFile.open(c);
  extern Wavenum_struct c;
  c.name = FilePathLine;
  //print_name(c);
  while(getline (readFile, lineToRead))
  {
      readFile >> value;
      c.Amp_k.push_back(value);
  }
  //print_amps(c);
}

除了您似乎正在重复使用变量名称" C"(这是不明显的)之外,您的Wavenum_struct c还会通过循环覆盖每个迭代,因此,即使您正确设置了此"外部",您将保留的大多数是最后一个。您必须在某个地方保存这些波数,所以为什么不使用vector

std::vector<Wavenum_struct> wavenums;
for (int lineCounter=0; lineCounter < numLines; lineCounter++)
{
  ...
  //print_amps(c);
  wavenums.push_back(c);
}