Linux、Window下INI文件的读写操作C++类

您所在的位置:网站首页 linux重新读取配置文件 Linux、Window下INI文件的读写操作C++类

Linux、Window下INI文件的读写操作C++类

2024-07-12 00:30| 来源: 网络整理| 查看: 265

1、适用于C++项目,适用于Windows和Linux平台,依赖STL标准库。 2、用于C++读取INI文件,写入INI文件,以及查找相应键值对的快速操作。 3、在项目中需要此功能时,只需要引入两个文件,即:INIParser.h和INIParser.cpp。

INIParser.h

#ifndef INI_PARSER_H #define INI_PARSER_H #include #include #include #include #include #include using namespace std; //其他部分省略 //主要的INIParser类 class INIParser { public: int ReadINI(string path); string GetValue(string root, string key); vector::size_type GetSize(){return map_ini.size();} vector::size_type SetValue(string root, string key, string value); int WriteINI(string path); void Clear(){map_ini.clear();} private: map map_ini; }; #endif // INI_PARSER_HINIParser.cpp

#include "iniparser.h" //remove all blank space string &TrimString(string &str) { string::size_type pos = 0; while(str.npos != (pos = str.find(" "))) str = str.replace(pos, pos+1, ""); return str; } //read in INI file and parse it int INIParser::ReadINI(string path) { ifstream in_conf_file(path.c_str()); if(!in_conf_file) return 0; string str_line = ""; string str_root = ""; vector vec_ini; while(getline(in_conf_file, str_line)) { string::size_type left_pos = 0; string::size_type right_pos = 0; string::size_type equal_div_pos = 0; string str_key = ""; string str_value = ""; if((str_line.npos != (left_pos = str_line.find("["))) && (str_line.npos != (right_pos = str_line.find("]")))) { //cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3