C++ 使用运行时生成的名称创建结构,并在程序中引用它们
我有一组数据文件存储在一个目录中。 例如 我的代码最初生成这些文件的路径列表C++ 使用运行时生成的名称创建结构,并在程序中引用它们,c++,struct,runtime,C++,Struct,Runtime,我有一组数据文件存储在一个目录中。 例如 我的代码最初生成这些文件的路径列表 std::string fileStringSearch="Fourier"; std::stringstream resultFileName; std::vector<std::string> fileList; int numLines=0; DIR *parentDirPointer; struct dirent *dp; if ((parentDirPointer = opendir("FT"
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();
std::string fileStringSearch=“Fourier”;
std::stringstream结果文件名;
std::矢量文件列表;
int numLines=0;
DIR*parentDirPointer;
结构方向*dp;
if((parentDirPointer=opendir(“FT”))==NULL)
{
std::cout在这个函数中(实际上应该有一个名称):
extern Wavenum\u struct c;
为什么extern
?您也在那里重新定义c
。您不需要到处都使用struct
关键字(仅在Wavenum\u struct
的定义中)。你为什么不直接使用向量
来存储所有文件数据呢?我根本不明白这与类型名有什么关系。啊,那是后来为了使结构的名称可以引用而做的努力-最初没有外部
。至于重新定义c
,我怀疑这就是我的问题所在,因为它是不可能(我想?)以后再参考它。我将从(struct Wavenum_struct t)
等中删除结构
,谢谢。对于“为什么不使用向量
来存储所有文件数据”的问题,你的意思是以某种方式而不是读取文件>>值;c.Amp_k.推回(值)
section?@Beta在下面的回答中概述了向量的用法。这允许我以后使用wavenums[0到numLines之间的值]。如果struct包含函数void print_name(){std::cout,则打印_name();
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);
}
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);
}
print_amps(reinterpret_cast<Wavenum_struct*>("FT/Fourier_1"));
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);
}
std::vector<Wavenum_struct> wavenums;
for (int lineCounter=0; lineCounter < numLines; lineCounter++)
{
...
//print_amps(c);
wavenums.push_back(c);
}