c++ - 从配置文件中读取变量

标签 c++

我正在尝试确定读取配置文件的最佳方式。这个“Parameters.cfg”文件只是为了定义值并且是这种形式:

origin_uniform_distribution 0
origin_defined 1
angles_gaussian 0
angles_uniform_distribution 0
angles_defined 0
startx 0
starty 0
gap 500
nevents 1000
origin_uniform_distribution_x_min -5
origin_uniform_distribution_x_max 5
origin_uniform_distribution_y_min -5
origin_uniform_distribution_y_max 5
origin_defined_x 0
origin_defined_y 0
angles_gaussian_center 0
angles_gaussian_sigma 5
angles_uniform_distribution_x_min -5
angles_uniform_distribution_x_max 5
angles_uniform_distribution_y_min -5
angles_uniform_distribution_y_max 5
angles_defined_x 10
angles_defined_y 10

名称是为了让用户知道他们正在定义哪些变量。我想让我的程序只读入实际数字并跳过字符串。我知道我可以通过在我的程序中定义大量字符串的方式来做到这一点,然后让它们坐在那里定义但显然未使用。有没有一种方法可以在跳过字符串的同时轻松读入数字?

最佳答案

显而易见的解决方案有什么问题?

string param_name;
int param_value;

while ( fin >> param_name >> param_value )
{
  ..
}

您可以在每次迭代后丢弃 param_name,同时将 param_value 存储在您需要的任何地方。

关于c++ - 从配置文件中读取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15099516/

相关文章:

c++ "no appropriate default constructor available"错误使用模板类数据成员

c++ - 错误 : '_mm_prefetch' was not declared in this scope

c++ - Minizip - 使用带有 UTF-8 文件名的 fopen

c++ - 如何使用鼠标钩子(Hook)让当前窗口永远看不到特定的鼠标消息?

c++ - 为什么我不能在初始化列表中初始化静态变量?

c++ - 如何将二进制 std::string 插入 BLOB

C++ 特化多模板参数包

C++ 重构 : conditional expansion and block elimination

c++ - K阶统计搜索

c++ - MSVCR80.DLL 丢失。我应该安装什么或者我应该在 c++ 项目配置中欺骗什么