创建具有在运行时生成的名称的结构,并在程序中引用它们
Create structs with names generated at runtime and refer to them within program
我在目录中存储了一组数据文件。例如
./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);
}
相关文章:
- 如何在 64 位 vb.net Windows 应用程序中引用 32 位 dll
- 不同于按值传递和常量引用传递的程序集
- 为什么从 constexpr 引用生成的程序集代码与从 constexpr 指针生成的程序集代码不同?
- 64 位进程中的 AnyCPU C# DLL 无法引用 64 位C++ DLL(给出错误:无法加载文件或程序集)
- C++ 为什么在定义的编译和链接之前引用外部实例的程序
- 在Linux/Ubuntu中编译openCV应用程序时,如何解决未定义的引用cv:..?
- 在Qt应用程序中包含Python.h会导致对Qt函数的未定义引用
- printf(时差程序)中的引用问题
- 编译简单的多文件类程序时出现未定义的引用错误
- Android (ART) 崩溃并显示错误 JNI 在应用程序中检测到错误:jstring 是无效的本地引用
- 错误:尝试在全新系统安装中构建应用程序后未定义对'...'的引用
- Cmake:如何引用链接到多个程序的目标文件中的文件?
- 通过查看程序集来比较按值传递与按引用传递性能
- 在 C++ 中调用时对内联程序集代码的未定义引用
- C++程序调用 C 函数 在 g++ 链接期间获取未定义的引用
- 由非托管 EXE 引用时托管 DLL 的应用程序配置
- r-工作程序中对自定义函数的未定义引用(C++和RcppParallel)
- 为什么通过引用传递从 std::function 派生的对象会使程序崩溃?
- 我需要做什么才能从 C# 应用程序引用 c++ dll
- 简单程序引用错误