你的位置:东莞市斯宇自动化设备有限公司 > 精密振动产品 > Ratel 网络库之设置文件模块策画思路
Ratel 网络库之设置文件模块策画思路
- 发布日期:2022-08-07 13:19 点击次数:183

设置文件
以 '#' 起头的每一行设置应应看成提示内容,在读取时该当间接跳过 当读取到 “[” 标志时,该当举行跳过方便我们对设置文件的分组 其次该当留心到对空格的过滤,预防空格对我们读取举行纷扰扰攘侵略 设置类的运行思路:CConfig
代码以下(含详细注释):
/* *CConfig.h */ #include<iostream> #include<map> class CConfig { public: //设置文件的门路 CConfig(std::string filename = "test.conf"); ~CConfig(void); //初始化设置 int InitConfig(); //取得设置参数 std::string getParam(std::string param); //设置设置参数 std::string setParam(); private: bool file_isok; //文件是否读取告成 int ParamHandle(std::string strline); //处理惩罚每一行的参数 public: std::map<std::string,std::string> paramstore; // 生活生涯全体参数 std::string filename; //文件名 };
/* * CConfig.cpp */ #include "stdafx.h" #include "Config.h" #include<fstream> #include <string> #include<cstring> CConfig::CConfig(std::string filename):file_isok(false) { std::ifstream fp; fp.open(filename.c_str(),std::ios::in); if(fp.is_open()){ //验证文件是否可以或许关上,并举行标志 file_isok = true; this->filename = filename; } } CConfig::~CConfig(void) { } std::string CConfig::getParam(std::string param){ for(auto pa : paramstore){ if(pa.first == param) return pa.second; } return ""; } int CConfig::InitConfig(){ if(!file_isok)return -1; //初始化失利 std::ifstream fp; fp.open(filename.c_str(), std::ifstream::in); if(!fp){ std::cerr << "文件读取犯错" << std::endl; return -1; } char str[100]; while(fp.getline(str,100)){ //读取每一行的设置文件 unsigned int start, end; for(start = 0; start < strlen(str); start++){ if(str[start] == ' '
相关资讯