C++ c++;:头文件中的配置变量

C++ c++;:头文件中的配置变量,c++,header-files,C++,Header Files,我有一个cpp代码,它使用头文件中的配置变量进行一些计算。我的程序结构如下 #include configuration_variables.h #include <necessary headers> void main(){ <code> } 配置变量.h const int start = 5; const int end = 10; int detectors[6] = {2, 3, 4, 6, 7, 8}; string filters[2]

我有一个cpp代码,它使用头文件中的配置变量进行一些计算。我的程序结构如下

#include configuration_variables.h
#include <necessary headers>

void main(){
    <code>
}
配置变量.h

const int start = 5;
const int end   = 10;

int detectors[6]  = {2, 3, 4, 6, 7, 8};
string filters[2] = {"Cd", "Pb"};
<more variables>
<and more variables>
const int start=5;
常数int end=10;
int检测器[6]={2,3,4,6,7,8};
字符串过滤器[2]={“Cd”,“Pb”};
当试图用不同的输入(即配置变量)解决同一问题时,配置变量和头文件的名称可能会发生变化,这意味着我必须重新编译代码


是否有方法每次读取不同的配置文件;需要什么并解析主代码中的变量?我认为这可能比我目前正在做的更优雅,但配置变量不仅仅是标量,因此文本处理(即使用awk)可能没有那么容易。

除了一些重新定义错误之外,我看不出您的操作有什么问题。将这些变量标记为
inline
,否则它们将被重新定义多次,因此可能会导致一些错误。如果它们是常量,请将它们标记为
const
constepr

有没有办法读取配置文件并解析主代码中的变量


如果我理解正确,是的,您可以使用文件来初始化它们。将变量存储在
.config
.txt
文件中,然后解析它并用数据初始化变量。我不确定它是否有很多好处,因为加载文件和初始化值需要花费一些性能。如果您在标题中预定义了它,那么编译器会处理它。

除了一些重新定义的错误之外,我看不出您的操作有什么问题。将这些变量标记为
inline
,否则它们将被重新定义多次,因此可能会导致一些错误。如果它们是常量,请将它们标记为
const
constepr

有没有办法读取配置文件并解析主代码中的变量


如果我理解正确,是的,您可以使用文件来初始化它们。将变量存储在
.config
.txt
文件中,然后解析它并用数据初始化变量。我不确定它是否有很多好处,因为加载文件和初始化值需要花费一些性能。如果您在头文件中预定义了输入,那么编译器将处理它。

您可以通过两种方式将输入传递给程序:使用命令行参数,或在文件中。从您拥有的变量数量来看,最好将值存储在文件中,然后通过命令行参数传递该文件。听起来像是要传入头文件本身。您仍然可以做到这一点,但解析C/C++语法并不可取。最好以不同的格式保存参数(比如JSON、CSV,甚至是您自己的格式)

根据程序的复杂程度,您可以做很多事情。但是,如果您想让一切都简单,请将配置文件作为程序的位置参数,然后根据您为程序决定的任何格式对其进行解析。下面是一个例子:

#include <fstream>
#include <string>

struct config{
  int start;
  int end;
  int detectors[6];
  std::string filters[2];
};


struct config parse_file(char * file_name){
   std::ifstream file;
   file.open(file_name, std::ifstream::in);
   // now parse the file using >> operators
   // if you aren't guaranteed to get a properly formatted file
   // you should check for incorrect format here and handle appropriately
   struct config param;
   file >> param.start;
   file >> param.end;
   for(int i = 0 ; i < 5 ; i++){
      file >> param.detectors[i];
   }
   file >> param.filters[0];
   file >> param.filters[1];
   return param;
}

int main(int argc, char **argv){
    if(argc < 2 || argc >= 3){
        // deal with no input/more than expected input
        exit(0);
    }
    struct config params = parse_file(argv[1]); // argv[0] stores binary name
    //use params as needed
}
#包括
#包括
结构配置{
int启动;
内端;
int探测器[6];
std::字符串过滤器[2];
};
结构配置解析文件(字符*文件名){
std::ifstream文件;
打开(文件名,std::ifstream::in);
//现在使用>>操作符解析文件
//如果不能保证获得正确格式的文件
//您应该检查此处的格式是否不正确,并进行适当处理
结构配置参数;
文件>>param.start;
文件>>param.end;
对于(int i=0;i<5;i++){
文件>>参数检测器[i];
}
文件>>参数过滤器[0];
文件>>参数过滤器[1];
返回参数;
}
int main(int argc,字符**argv){
如果(argc<2 | | argc>=3){
//处理无输入/超出预期的输入
出口(0);
}
struct config params=parse_file(argv[1]);//argv[0]存储二进制名称
//根据需要使用参数
}
您可以根据用例的需要使其更加复杂。例如,您可以使用库或c标准库传递此配置文件,以及程序的其他参数,如标准unix工具。还有解析标准文件格式(如CSV、JSON、XML)的库。我通过快速搜索找到的示例:


如果你想坚持使用头文件,我发现这会解析C++头文件并将它们格式化为JSON。这可能有点过分,因为它还可以解析类和模板。您需要修改它以将其与代码集成。

您可以通过两种方式将输入传递给程序:使用命令行参数或在文件中。从您拥有的变量数量来看,最好将值存储在文件中,然后通过命令行参数传递该文件。听起来像是要传入头文件本身。您仍然可以做到这一点,但解析C/C++语法并不可取。最好以不同的格式保存参数(比如JSON、CSV,甚至是您自己的格式)

根据程序的复杂程度,您可以做很多事情。但是,如果您想让一切都简单,请将配置文件作为程序的位置参数,然后根据您为程序决定的任何格式对其进行解析。下面是一个例子:

#include <fstream>
#include <string>

struct config{
  int start;
  int end;
  int detectors[6];
  std::string filters[2];
};


struct config parse_file(char * file_name){
   std::ifstream file;
   file.open(file_name, std::ifstream::in);
   // now parse the file using >> operators
   // if you aren't guaranteed to get a properly formatted file
   // you should check for incorrect format here and handle appropriately
   struct config param;
   file >> param.start;
   file >> param.end;
   for(int i = 0 ; i < 5 ; i++){
      file >> param.detectors[i];
   }
   file >> param.filters[0];
   file >> param.filters[1];
   return param;
}

int main(int argc, char **argv){
    if(argc < 2 || argc >= 3){
        // deal with no input/more than expected input
        exit(0);
    }
    struct config params = parse_file(argv[1]); // argv[0] stores binary name
    //use params as needed
}
#包括
#包括
结构配置{
int启动;
内端;
int探测器[6];
std::字符串过滤器[2];
};
结构配置解析文件(字符*文件名){
std::ifstream文件;
打开(文件名,std::ifstream::in);
//现在使用>>操作符解析文件
//如果不能保证获得正确格式的文件
//您应该检查此处的格式是否不正确,并进行适当处理
结构公司