zoukankan      html  css  js  c++  java
  • c++ 读取配置文件方法

    一。消息头文件内容如下:

    // stdafx.h : 标准系统包含文件的包含文件,
    // 或是经常使用但不常更改的
    // 特定于项目的包含文件
    //

    #pragma once

    #include "targetver.h"
    #include <stdio.h>
    #include <tchar.h>

    #ifndef _GET_CONFIG_H_
    #define _GET_CONFIG_H_
     
    #include <string>
    #include <map>
    using namespace std;
     
    #define COMMENT_CHAR '#'
     
    bool ReadConfig(const string & filename, map<string, string> & m);
    void PrintConfig(const map<string, string> & m);
    #endif

    二。 代码:

    // operatorXmlFile.cpp : 定义控制台应用程序的入口点。
    //

    #include "stdafx.h"
    #include <fstream>
    #include <iostream>
    using namespace std;

    bool IsSpace(char c)
    {
        if (' ' == c || ' ' == c)
            return true;
        return false;
    }

    void Trim(string & str)
    {
        if (str.empty()) {
            return;
        }
        int i, start_pos, end_pos;
        for (i = 0; i < str.size(); ++i) {
            if (!IsSpace(str[i])) {
                break;
            }
        }
        if (i == str.size()) { // 全部是空白字符串
            str = "";
            return;
        }
        
        start_pos = i;
        
        for (i = str.size() - 1; i >= 0; --i) {
            if (!IsSpace(str[i])) {
                break;
            }
        }
        end_pos = i;
        
        str = str.substr(start_pos, end_pos - start_pos + 1);
    }

    bool AnalyseLine(const string & line, string & key, string & value)
    {
        if (line.empty())
            return false;
        int start_pos = 0, end_pos = line.size() - 1, pos;
        if ((pos = line.find(COMMENT_CHAR)) != -1) {
            if (0 == pos) {  // 行的第一个字符就是注释字符
                return false;
            }
            end_pos = pos - 1;
        }
        string new_line = line.substr(start_pos, start_pos + 1 - end_pos);  // 预处理,删除注释部分
        
        if ((pos = new_line.find('=')) == -1)
            return false;  // 没有=号
            
        key = new_line.substr(0, pos);
        value = new_line.substr(pos + 1, end_pos + 1- (pos + 1));
        
        Trim(key);
        if (key.empty()) {
            return false;
        }
        Trim(value);
        return true;
    }

    bool ReadConfig(const string & filename, map<string, string> & m)
    {
        m.clear();
        ifstream infile(filename.c_str());
        if (!infile) {
            cout << "file open error" << endl;
            return false;
        }
        string line, key, value;
        while (getline(infile, line)) {
            if (AnalyseLine(line, key, value)) {
                m[key] = value;
            }
        }
        
        infile.close();
        return true;
    }

    void PrintConfig(const map<string, string> & m)
    {
        map<string, string>::const_iterator mite = m.begin();
        for (; mite != m.end(); ++mite) {
            cout << mite->first << "=" << mite->second << endl;
        }
    }

    int main()
    {
        map<string, string> m;
        ReadConfig("cfg.txt", m);
        PrintConfig(m);
         
        system("pause");
        return 0;
    }

    三。 配置文件名称为 cfg.txt 内容如下:

    root = etc
    config  =  etc/profile
    ProGroupName = branchreceive

    四。执行结果截图

  • 相关阅读:
    c++ 用宏代替常用的函数
    爬取网易云音乐(包括歌词和评论)
    三种常见的单例模式
    函数式编程filter和map的区别
    四种常见排序算法(快速,冒泡,插入,选择排序)
    6.微信撤回消息的获取
    5.微信拜年短信自动回复
    4.深拷贝和浅拷贝
    3.迭代器以及迭代器的作用
    2.生成器计算出斐波那契数列
  • 原文地址:https://www.cnblogs.com/northeastTycoon/p/9294875.html
Copyright © 2011-2022 走看看