c++配置⽂件读取、修改、添加cfg.h
#pragma once
#include <iostream>
#include <string>
#include <cstring>
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 <fstream>
//构造函数
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->keypare(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 (!f())
{
//初始化缓存
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->keypare(key) << endl; //到值则直接改变
if (cur->keypare(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 <iostream>
#include "cfg.h"
using namespace std;
#define FILE_PATH ""
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 << Lines() << endl;
王中磊背景cout << "name=" << CFG("name") << endl; cout << CFG("cxx") << endl;
system("pause");
}
发布评论