我在哪裏可以找到用於讀取和操作Unix配置文件的C或C++庫(format: name=value\n
)?C/C++ Unix配置文件庫
回答
我會建議你使用C++的boost :: property_tree庫。它有安靜的詳細手冊。此外,我會建議你使用「信息」配置文件。配置文件的
例子:
; 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;
}
如果你想使用strcitly(name =「value」)格式,boost也支持它。只需使用ini_parser而不是info_parser。 – beduin 2011-04-18 10:16:55
這似乎是我正在尋找的解決方案。然而,我認爲我將首先推出自己的解決方案(用於教育目的),然後在以後的項目中轉向提升。謝謝! – Spliffster 2011-04-18 10:20:08
我已經寫了一個配置解析器「信息」的風格配置文件我自己几几周前。這是完全符合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.*
here。
我很樂意根據需要添加文檔和/或界面。
感謝您的示例代碼。因爲我只有幾天進入C這將幫助我理解文件和字符串處理。 – Spliffster 2011-04-18 10:21:38
在Augeas看看,它相當普遍。
- 1. UNIX cc可執行文件位置
- 2. Linux配置文件庫
- 3. Erorr庫配置文件
- 4. .gitignore un-ignore .cc文件
- 5. Unix sed XML配置編輯
- 6. Unix腳本無法更改Ubuntu上的postgres hba.conf配置文件
- 7. 閱讀unix框上配置xml文件的最佳方式
- 8. 有沒有關於Unix配置文件的指南
- 9. 解析/更新UNIX配置文件的API
- 10. 笨定製庫和配置文件
- 11. 會員,角色和配置文件庫
- 12. 配置文件vs數據庫表
- 13. owlim庫使用配置文件
- 14. Cross Git存儲庫配置文件
- 15. 企業庫的外部配置文件
- 16. 保護nodeJS數據庫配置文件
- 17. 爲存儲庫配置beans文件
- 18. asp.net從類庫讀取配置文件
- 19. 跨庫的多個配置文件
- 20. 配置文件或數據庫?
- 21. Linux與Unix文件通配符
- 22. 文件名匹配Windows與Unix
- 23. Python中的Unix文件名通配符?
- 24. 如何在Eclipse中爲啓動新項目時創建的文件將.cc文件擴展名設置爲.cc?
- 25. 配置文件
- 26. 配置文件
- 27. 配置在unix程序中的位置
- 28. 用法:cc [options]文件。使用'cc -flags'作爲細節
- 29. Vim插件clang_complete看不到* .cc文件
- 30. 如何在UNIX中轉置文件?
C(或C++)標準庫? – pmg 2011-04-18 08:50:24
我確定有人會提到C++的'boost :: program_options'和C的'getopt'。 – Nim 2011-04-18 08:50:51
對不起,這可能聽起來像一個愚蠢的問題,我對C/C++非常陌生。 – Spliffster 2011-04-18 08:51:32