C/C++知识点之c++配置文件读取、修改、添加
小标 2018-12-03 来源 : 阅读 3427 评论 0

摘要:本文主要向大家介绍了 C/C++知识点之c++配置文件读取、修改、添加,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了 C/C++知识点之c++配置文件读取、修改、添加,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

cfg.h


#pragma once
#include 
#include 
#include 
using namespace std;

struct CFG_J
{
    string key;//索引
    string value;//值
    CFG_J *next;//下个结点
};
class Config
{
private:
    string file_name;//文件名字
    CFG_J * head;//头指针
    int cfg_line;//配置行数
    int createHead();//创建一个链表头指针
    int freeJoin();//释放链表的节点
    int inputFile();//内存配置同步配置到文件
    int joinHead(string key, string value);//将某个配置加入到链表中
public:
    Config(string file_name);//构造函数
    ~Config();//析构函数
    int getLines();//获取配置数量
    int setCFG(string key, string value);//设置一个配置
    string getCFG(string key);//从内存获取某个配置的值
    int getCFG();//从文件获取所有的配置 加载入内存链表
    void printCfg();//打印配置链表内容
};

cfg.cpp

#include "cfg.h"
#include 

//构造函数
Config::Config(string file_name)
{
    //定义一个配置文件
    this->file_name = file_name;
    //默认一共0个配置
    this->cfg_line = 0;
    if (createHead() == 0)
    {
        //从文件读取全部配置 加入链表
        getCFG();
        //打印全部配置
        //printCfg();
    }
}

//析构函数
Config::~Config()
{
    //释放链表的各个节点
    freeJoin();
    //释放头节点
    if (this->head != NULL)
    {
        delete this->head;
    }
}

//获取配置的总数
int Config::getLines()
{
    return this->cfg_line;
}

//设置某个配置项
int Config::setCFG(string key, string value)
{
    int rt = 0;
    //插入链表
    joinHead(key, value);
    //同步到文件
    inputFile();
    return rt;
}

//内存配置同步到文件
int Config::inputFile()
{
    int rt = 1;
    if (this->head == NULL)
    {
        return rt;
    }
    //缓存字符串
    string st;
    //定义文件类型
    ofstream outfile;
    //打开文件方式
    outfile.open(this->file_name , ios::out | ios::trunc);
    // 遍历向文件写入用户输入的数据
    CFG_J * p = this->head->next;
    while (p != NULL)
    {
        //定义字符串
        st = p->key + "=" + p->value;
        //写入字符串
        outfile << st << endl;
        //移动指针
        p = p->next;
    }
    //关闭文件
    outfile.close();
    return rt;
}

//获取某项特定配置
string Config::getCFG(string key)
{
    //默认找不到
    string rt = "CANNOT FIND THIS CONFIG.";
    if (this->head == NULL)
    {
        return rt;
    }
    //遍历抓取配置项
    CFG_J *p = this->head->next;
    while (p != NULL)
    {
        if (p->key.compare(key) == 0)
        {
            //捕捉到则返回值
            rt = p->value;
            break;
        }
        p = p->next;
    }
    return rt;
}

//从文件获取全部的配置
int Config::getCFG()
{
    int rt = 0;
    //先清空链表
    freeJoin();
    //配置总数为0
    this->cfg_line = 0;
    //定义缓存字符串变量
    string st;
    string key, value;
    //定义文件变量
    ifstream infile;
    string::size_type idx;
    char *p = NULL, *q = NULL;
    //打开文件
    infile.open(this->file_name);
    //遍历直到文件的最后
    while (!infile.eof())
    {
        //初始化缓存
        st = "";
        //取得一行配置
        infile >> st;
        //找不到等号则继续
        idx = st.find("=");
        if (idx == string::npos)
        {
            continue;
        }
        //截断字符串得到key和value字符串
        key = st.substr(0, idx);
        value = st.substr(idx + 1,st.length()-idx);
        //插入链表
        joinHead(key,value);
    }
    //关闭文件
    infile.close();
    return rt;
}

//将配置插入内存链表
int Config::joinHead(string key,string value)
{
    int rt = 1;
    if (this->head == NULL)
    {
        rt = 2;
        return rt;
    }
    //定义移动指针
    CFG_J * p = this->head;
    CFG_J * cur = p->next;
    while (cur != NULL)
    {
        //cout << cur->key << " " << key << " " << cur->key.compare(key) << endl;
        //找到值则直接改变
        if (cur->key.compare(key) == 0)
        {
            cur->value = value;
            rt = 0;
            break;
        }
        p = cur;
        cur = p->next;
    }
    //找不到值则再最后插入
    if (rt != 0)
    {
        CFG_J *q = new CFG_J();
        q->key = key;
        q->value = value;
        q->next = NULL;
        p->next = q;
        //配置数自增
        this->cfg_line ++;
    }
    return rt;
}

//释放全部节点(除了头指针)
int Config::freeJoin()
{
    int rt = 1;
    //定义移动指针
    CFG_J * p = this->head->next;
    CFG_J * cur = p;
    if (p == NULL)
    {
        return rt;
    }
    //遍历释放内存
    while (cur != NULL)
    {
        p = cur->next;
        delete cur;
        cur = p;
    }
    //初始化头指针
    this->head->next = NULL;
    rt = 0;
    return rt;
}

//打印所有的配置
void Config::printCfg()
{
    if (this->head == NULL)
    {
        return;
    }
    //定义移动指针
    CFG_J * p = this->head->next;
    while (p != NULL)
    {
        cout << p->key << "=" << p->value << endl;
        //移动指针
        p = p->next;
    }
}

//创建配置链表头指针
int Config::createHead()
{
    int rt = 1;
    CFG_J *p = new CFG_J();
    p->key = "headkey";
    p->value = "headvalue";
    p->next = NULL;
    this->head = p;
    rt = 0;//没有问题
    return rt;
}

main.cpp

#include 
#include "cfg.h"
using namespace std;

#define FILE_PATH "cfg.txt"

int main()
{
    string key;
    string value;
    Config cfg(FILE_PATH);
    /*cout << "Writing to the file" << endl;
    cout << "Enter your key: ";
    cin >> key;
    cout << "Enter your value: ";
    cin >> value;
    cfg.setCFG(key,value);*/
    cout << cfg.getLines() << endl;
    cout << "name=" << cfg.getCFG("name") << endl;
    cout << cfg.getCFG("cxx") << endl;
    system("pause");
}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved