zoukankan      html  css  js  c++  java
  • c++读写配置文件

    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 << "
    ";
        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...
    ";
                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...
    ";
                    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] << ")
    ";
    
                if ( !fin.good() ){
                  cout<<"
    ";
                  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] << '
    ';
            if (iter == this->data.end())
            {
                cerr<<"    Parameter name "<< key <<" not found!"<<endl;
                return string("NOT_FOUND");
            }
            return iter->second;
        }
    public:
        map<string, string> data;
    };
  • 相关阅读:
    ASP.NET通用权限管理系统(FrameWork) 1.0.0 Release 发布
    ASP.NET通用权限管理系统(FrameWork) 1.0.3 Release
    ASP.NET通用权限管理系统(FrameWork) 之用户在线列表 泛型缓存 [原创]
    DDBuildToolsRelease1.0
    FineMessBox弹出窗口js (修改版,增加对Firefox支持)
    [开源]ASP.NET通用权限管理系统(FrameWork) 1.0.2 Release
    [开源]ASP.NET通用权限管理系统(FrameWork) 1.0.1 Release
    如何在CodePlex 创建开源项目
    今天上班遇到好人了
    ASP.NET通用权限管理系统(FrameWork) 在线演示地址
  • 原文地址:https://www.cnblogs.com/chaofn/p/11718979.html
Copyright © 2011-2022 走看看