当前位置: 代码迷 >> 综合 >> windows c++读写配置文件
  详细解决方案

windows c++读写配置文件

热度:88   发布时间:2023-10-31 14:08:26.0
  1. config_file.h
    #pragma once
    #include <list>
    #include <map>
    #include <string>
    #include <fstream>
    #include <corecrt_io.h>
    #include "BASE64_API.h"
    using namespace std;//配置文件读取工具int ConfigFileInit(map<string, string>& m_mapConfigInfo, string path);//初始化配置文件,map存放所有信息,path为路径
    string getValueString(map<string, string> m_mapConfigInfo, string key);//获取字符串值
    int getValueInt(map<string, string> m_mapConfigInfo, string key);//获取整型值
    int write_conf(string path, string key, string value);//写入配置
    int delete_conf(string path, string key);//删除配置
    int change_conf(string path, string key, string value);//修改配置

     

  2. config_file.cpp
#include "config_file.h"int ConfigFileInit(map<string, string>& m_mapConfigInfo, string path)
{ifstream configFile;configFile.open(path.c_str());string str_line;if (configFile.is_open()){while (!configFile.eof()){getline(configFile, str_line);if (str_line.find('#') == 0) //过滤掉注释信息,即如果首个字符为#就过滤掉这一行{continue;}size_t pos = str_line.find('=');string str_key = str_line.substr(0, pos);string str_value = str_line.substr(pos + 1);m_mapConfigInfo.insert(pair<string, string>(str_key, str_value));}}else{return -1;}configFile.close();return 1;
}string getValueString(map<string, string> m_mapConfigInfo, string key) {map<string, string>::iterator iter_configMap;iter_configMap = m_mapConfigInfo.find(key);if (iter_configMap != m_mapConfigInfo.end()) {return iter_configMap->second;}return "";
}int getValueInt(map<string, string> m_mapConfigInfo, string key) {string s = getValueString(m_mapConfigInfo, key);char* end;int i = static_cast<int>(strtol(s.c_str(), &end, 10));return i;
}int write_conf(string path, string key, string value) {ofstream write;write.open(path, ios::app);                //用ios::app不会覆盖文件内容if (!write.is_open()) {//return -1;} write << endl;write << key + "=" + value;write.close();return 1;
}int delete_conf(string path, string key) {ifstream in;in.open(path);if (!in.is_open()) return -1;string strFileData = "";string str_line = "";while (!in.eof()){getline(in, str_line);size_t pos = str_line.find('=');string str_key = str_line.substr(0, pos);if (str_key == key || str_line == "\n" || str_line == "" || str_line == " ") //过滤掉注释信息,即如果首个字符为#就过滤掉这一行{//strFileData += "\n";}else {strFileData += str_line;strFileData += "\n";}}in.close();//写入文件ofstream out;out.open(path);out.flush();out << strFileData;out.close();return 1;
}int change_conf(string path, string key, string value) {delete_conf(path, key);write_conf(path, key, value);return 1;
}

 3. 使用示例

#define CONFIG_PATH "setting.conf"int main() {map<string, string> m_mapConfigInfo;ConfigFileInit(mapConfigInfo, CONFIG_PATH);//读取配置文件string bad_urls = getValueString(mapConfigInfo, "logPath");//获取配置文件的值//change_conf(CONFIG_PATH, "logPath", "C:\b\a\d.txt");//delete_conf(CONFIG_PATH, "logPath");//write_conf(CONFIG_PATH, "logPath", "C:\b\a\d.txt");
}

4. 配置文件示例

#日志文件
logPath=C:\a\b\c.log

5. 注意:配置文件最好不包含空格,想去掉空格的自己简单改一下

  相关解决方案