C/C++;Unix配置文件库 P>我可以在哪里找到一个C++和C++库来读取和操作UNIX配置文件(格式:name = Value\n>代码>?)< /p> < p>我建议您使用Boo::它有详细的手册。此外,我建议您使用“info”配置文件

C/C++;Unix配置文件库 P>我可以在哪里找到一个C++和C++库来读取和操作UNIX配置文件(格式:name = Value\n>代码>?)< /p> < p>我建议您使用Boo::它有详细的手册。此外,我建议您使用“info”配置文件,c++,c,unix,configuration-files,C++,C,Unix,Configuration Files,配置文件的示例: ; this is just comment line firstParamSection { stringParam "string" intParam 10 } #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/info_parser.hpp> #include <string> int main (int argc, char

配置文件的示例:

; this is just comment line

firstParamSection 
{
   stringParam "string"
   intParam 10
}
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <string>

int main (int argc, char *argv[]) {
  std::string testString;
  int testInt;

  boost::property_tree::ptree pTree;
  try {
    read_info("test/config/file/name", pTree);
  }
  catch (boost::property_tree::info_parser_error e) {
    std::cout << "error" << std::endl;
  }

  try {
    testString = pTree.get<std::string>("firstParamSection.stringParam");
    testInt = pTree.get<int>("firstParamSection.intParam");
  }

  catch(boost::property_tree::ptree_bad_path e) {
    std::cout << "error" << std::endl;
  }
从配置文件检索此参数的代码示例:

; this is just comment line

firstParamSection 
{
   stringParam "string"
   intParam 10
}
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/info_parser.hpp>
#include <string>

int main (int argc, char *argv[]) {
  std::string testString;
  int testInt;

  boost::property_tree::ptree pTree;
  try {
    read_info("test/config/file/name", pTree);
  }
  catch (boost::property_tree::info_parser_error e) {
    std::cout << "error" << std::endl;
  }

  try {
    testString = pTree.get<std::string>("firstParamSection.stringParam");
    testInt = pTree.get<int>("firstParamSection.intParam");
  }

  catch(boost::property_tree::ptree_bad_path e) {
    std::cout << "error" << std::endl;
  }
#包括
#包括
#包括
int main(int argc,char*argv[]){
std::string testString;
智力测验;
boost::property_tree::ptree ptree;
试一试{
读取信息(“test/config/file/name”,pTree);
}
捕获(boost::property\u tree::info\u parser\u error e){

std::cout几周前,我自己为“info”样式的配置文件编写了一个配置解析器。它完全兼容XDG,节可以嵌套,而且使用起来非常简单:

// read config file "barc" in directory $XDG_CONFIG_HOME/foo, e.g. /home/bwk/.config/foo/barc
config_read("foo", "barc");

// can read a specific file as well:
config_read_file("/etc/tralalarc");

// or from an open FILE *fp
config_read_fp(fp);

// or n characters directly from memory
config_read_mem(0xDEADBEEF, n);


// retrieve value associated with "key" in section "here", sub-section "my"
char *val = config_get("here.my.key");
您还可以设置/锁定配置变量(包括注释)并将配置写回磁盘。这是一个很好的自我解释,但缺少文档。请参阅
config.*


我很乐意根据需要添加文档和/或接口。

对于普通C来说,非常好。

看看,它非常通用。

C(或C++)标准库?我敢肯定有人会提到“C++的代码>选项/代码> <代码> GETopt < /Cord>。对不起,这听起来像是一个愚蠢的问题,我对C/C++很陌生。我正在编写一个UNIX ISH系统的程序。我需要一个简单的配置文件,它的行为就像*NIX管理员所期望的。如果你想使用StrutLy(Name)“value”)格式boost也支持它。只需使用ini_解析器而不是info_解析器。这似乎是我一直在寻找的解决方案。不过,我想我会首先推出自己的解决方案(出于教育目的)然后在以后的项目中切换到boost。谢谢!谢谢你的示例代码。因为我只学了几天C,这将帮助我理解文件和字符串处理。有趣的是,它看起来类似于puppetmaster使用的格式。