c ++ to read and write configuration file

1. Using the boost libraries

The Boost
the Boost library is a portable, providing the source code for C ++ library as a backup standard library is one of the development engines C ++ standardization process is to provide an extended number of C ++ libraries for C ++ language standard library in general. Boost library initiated by the C ++ standards committee Library Working Group members, some of which content is expected to become the next generation of C ++ Standard Library content. In the C ++ community impact is very large, it is truly a "quasi" standard library. Boost because of its emphasis on cross-platform, ++ emphasis on standard C, regardless of the platform to write. Most simply use the boost library functions include the appropriate header files needed to link library, a few (such as regular expression library, file systems, libraries, etc.). Here also uses file system library, more particular description of the venue and community links.
Community Links
boost official website: https://www.boost.org/
boost Filesystem: https://www.boost.org/doc/libs/1_70_0/libs/filesystem/doc/index.htm


Installation Boost
PC is ubuntu system, you can install the command, other systems have not tried. Of course, you can download the installation package for installation.

sudo apt-get install libboost-all-dev

Reads the configuration file

Configuration file as follows:

[System]
reboot_cnt=3

Read configuration code is as follows:

#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 ++ command

g++ -o test test.cc -lboost_system -lboost_filesystem

Modify the configuration file

Configuration file as follows:

[System]
reboot_cnt=3

Modify the configuration code is as follows:

#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 ++ command

g++ -o test test.cc -lboost_system -lboost_filesystem

Contents of the configuration file modified as follows:

[System]
reboot_cnt=10

Form to write the file to initialize the configuration file

Suppose the configuration file does not exist, the initialization code is as follows:

#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 ++ command

g++ -o test test.cc -lboost_system -lboost_filesystem

The contents of the configuration file after initialization is as follows:

[System]
reboot_cnt=5

Reads the entire file

Configuration file as follows:

[System]
reboot_cnt=3

Read the entire file code is as follows:

#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 * 40int main(int argc, char* argv[]) {
  if (!boost::filesystem::exists("config.ini")) {
    std::cerr << "config.ini not exists." << std::endl;
    return -1;Four
  }




  * 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 ++ command

g++ -o test test.cc -lboost_system -lboost_filesystem

 

 

Reprinted from the original: https://blog.csdn.net/u013736136/article/details/92843525

Guess you like

Origin www.cnblogs.com/chaofn/p/11718979.html