C++:删除ini配置文件中的键值(key)

/// ReadFile.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//--------------------------------------------------------------------------
/// strip 函数在博客“C++:strip函数以及读取ini配置文件”中,这里不再赘述
//--------------------------------------------------------------------------
#include "pch.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
//删除ini配置文件的key
void deleteKey_INI_File(string file_path, string name, string title, string key)
{
    ifstream i(file_path + "\\" + name);
    vector<string> lines;
    bool match_title = false;
    if (i.is_open())
    {
        while (!i.eof())
        {
            string line;
            getline(i, line);
            //初步清洗
            strip(line);
            //二次清洗
            if (line[0] == ';')
            {
                lines.push_back(line);
            }//注释不处理
            else
            {
                //未匹配title
                if (!match_title)
                {
                    if (line[0] == '[')
                    {
                        string title_temp = line;
                        strip(title_temp, '[', ']');
                        strip(title_temp);
                        if (title_temp.compare(title) == 0)
                        {
                            match_title = true;
                        }
                        lines.push_back("[" + title_temp + "]");
                    }//处理title
                    else
                    {
                        size_t pos = line.find_first_of('=');
                        if (pos != string::npos)
                        {
                            string key_temp = line.substr(0, pos);
                            string value_temp = line.substr(pos + 1, line.length());
                            strip(key_temp);
                            strip(value_temp);
                            lines.push_back(key_temp + "=" + value_temp);
                        }
                    }//处理key、value
                }
                //完成title匹配
                else
                {
                    //遇到下一个title
                    if (line[0] == '[')
                    {
                        lines.push_back(line);
                        match_title = false;
                    }//处理title
                    else
                    {
                        size_t pos = line.find_first_of('=');
                        if (pos != string::npos)
                        {
                            string key_temp = line.substr(0, pos);
                            string value_temp = line.substr(pos + 1, line.length());
                            strip(key_temp);
                            strip(value_temp);
                            //key不匹配则保存key,否则放弃
                            if (key_temp.compare(key) != 0)
                            {
                                lines.push_back(key_temp + "=" + value_temp);
                            }
                        }
                    }//处理key、value
                }
            }
        }
    }
    i.close();
    ofstream o(file_path + "\\" + name);
    int index = 0;
    int length = lines.size();
    while (length != index)
    {
        string line = lines[index];
        o << line.c_str() << endl;
        index++;
    }
    cout << "delete finish\n";
    o.close();
}

//主函数
int main()
{
    deleteKey_INI_File(".", "configure.ini", "title", "key");
    getchar();
}

 

上一篇:并发编程之:Lock


下一篇:python strip 函数用法及介绍