c++配置文件讀取、修改、添加
阿新 • • 發佈:2018-03-29
onf 默認 bsp sub 文件名 字符串變量 style col lines
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->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 <iostream> #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++配置文件讀取、修改、添加