1. 使用boost庫
Boost
Boost庫是一個可移植、提供源代碼的C++庫,作為標准庫的后備,是C++標准化進程的開發引擎之一,是為C++語言標准庫提供擴展的一些C++程序庫的總稱。 Boost庫由C++標准委員會庫工作組成員發起,其中有些內容有望成為下一代C++標准庫內容。在C++社區中影響甚大,是不折不扣的“准”標准庫。Boost由於其對跨平台的強調,對標准C++的強調,與編寫平台無關。大部分boost庫功能的使用只需包括相應頭文件即可,少數(如正則表達式庫,文件系統庫等)需要鏈接庫。這里也用到了文件系統庫,更具體的說明請移步社區鏈接。
社區鏈接
boost 官網:https://www.boost.org/
boost Filesystem:https://www.boost.org/doc/libs/1_70_0/libs/filesystem/doc/index.htm
安裝Boost
個人電腦是ubuntu系統,命令安裝即可,其他系統沒試過。當然也可以下載安裝包進行安裝。
sudo apt-get install libboost-all-dev
讀取配置文件
配置文件內容如下:
[System] reboot_cnt=3
讀取配置代碼如下:
#include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <iostream> int main(int argc, char* argv[]) { if (!boost::filesystem::exists("config.ini")) { std::cerr << "config.ini not exists." << std::endl; return -1; } boost::property_tree::ptree root_node, tag_system; boost::property_tree::ini_parser::read_ini("config.ini", root_node); tag_system = root_node.get_child("System"); if(tag_system.count("reboot_cnt") != 1) { std::cerr << "reboot_cnt node not exists." << std::endl; return -1; } int cnt = cnt = tag_system.get<int>("reboot_cnt"); std::cout << "reboot_cnt: " << cnt << std::endl; return 0; }
g++命令
g++ -o test test.cc -lboost_system -lboost_filesystem
修改配置文件
配置文件內容如下:
[System] reboot_cnt=3
修改配置代碼如下:
#include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <iostream> int main(int argc, char* argv[]) { if (!boost::filesystem::exists("config.ini")) { std::cerr << "config.ini not exists." << std::endl; return -1; } boost::property_tree::ptree root_node; boost::property_tree::ini_parser::read_ini("config.ini", root_node); root_node.put<int>("System.reboot_cnt", 10); write_ini("config.ini", root_node); return 0; }
g++命令
g++ -o test test.cc -lboost_system -lboost_filesystem
修改后的配置文件內容如下:
[System] reboot_cnt=10
寫文件的形式初始化配置文件
假設配置文件不存在,初始化代碼如下:
#include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <iostream> int main(int argc, char* argv[]) { if (!boost::filesystem::exists("config.ini")) { boost::filesystem::ofstream ofstream("config.ini", std::ios_base::out); ofstream << "[System]"; ofstream << "\n"; ofstream << "reboot_cnt=5"; ofstream.close(); } }
g++命令
g++ -o test test.cc -lboost_system -lboost_filesystem
初始化后的配置文件內容如下:
[System] reboot_cnt=5
讀取整個文件
配置文件內容如下:
[System] reboot_cnt=3
讀取整個文件代碼如下:
#include <boost/filesystem.hpp> #include <boost/filesystem/fstream.hpp> #include <boost/property_tree/ini_parser.hpp> #include <boost/property_tree/ptree.hpp> #include <iostream> #define FILE_MAX_SIZE 1024 * 40 int main(int argc, char* argv[]) { if (!boost::filesystem::exists("config.ini")) { std::cerr << "config.ini not exists." << std::endl; return -1; } char* data = (char*)malloc(sizeof(char) * FILE_MAX_SIZE); boost::filesystem::ifstream ifstream("config.ini", std::ios_base::in); ifstream.read(data, FILE_MAX_SIZE); std::cout << "data: " << std::endl; std::cout << data << std::endl; free(data); ifstream.close(); }
g++命令
g++ -o test test.cc -lboost_system -lboost_filesystem
原文轉載自:https://blog.csdn.net/u013736136/article/details/92843525
方法2
#ifndef paramReader_hpp #define paramReader_hpp #endif /* paramReader_hpp */ #include <fstream> #include <map> #include <vector> using namespace std; class ParameterReader { public: ParameterReader( string filename="parameters.txt" ) { ifstream fin( filename.c_str() ); if (!fin) { cerr<<"parameter file does not exist."<<endl; return; } while(!fin.eof()) { cout << "------ Reading in Parameter File...\r\n"; string str; getline( fin, str ); cout << " Line Read: " << str << endl; if (str[0] == '#') { continue; } int pos = str.find("="); if (pos == -1){ cout << "pos found = -1 ---- Continuing loop...\r\n"; continue; } string key = str.substr( 0, pos );
if(!key.empty())
{
key.erase(0,key.find_first_not_of(" "));
key.erase(key.find_last_not_of(" ") + 1);
}
string value = str.substr( pos+1, str.length());
if(!value.empty())
{
value.erase(0,value.find_first_not_of(" "));
value.erase(value.find_last_not_of(" ") + 1);
}
this->data[key] = value; cout << " Key Found with Value: " << key << " -> " << value << endl; cout << " Stored data mapping: key (" << key << ") ------- value(" << this->data[key] << ")\r\n"; if ( !fin.good() ){ cout<<"\r\n"; break; } } } string getData( string key ) { map<string, string>::iterator iter; iter = this->data.find(key.c_str()); std::cout << "Searching for key (" << key.c_str() << ") => " << this->data[key] << '\n'; if (iter == this->data.end()) { cerr<<" Parameter name "<< key <<" not found!"<<endl; return string("NOT_FOUND"); } return iter->second; } public: map<string, string> data; };
