1、普通文件读写
文件样式
#client listener
listenip=172.18.111.180
listenport=20000
#monitor listener
monitorlistenip=0.0.0.0
monitorlistenport=8888
monitortoken=123
#http listener
httplistenip=0.0.0.0
httplistenport=12345
logfiledir=logs/
logfilename=chatserver
logbinarypackage=0
#mysql config
dbserver=172.18.111.180
dbuser=root
dbpassword=123456
dbname=flamingo
0、头文件
#ifndef __CONFIG_FILE_READER_H__
#define __CONFIG_FILE_READER_H__
#include <map>
#include <string>
class CConfigFileReader
{
public:
CConfigFileReader(const char* filename);
~CConfigFileReader();
char* getConfigName(const char* name);
int setConfigValue(const char* name, const char* value);
private:
void loadFile(const char* filename);
int writeFile(const char* filename = NULL);
void parseLine(char* line);
char* trimSpace(char* name);
bool m_load_ok;
std::map<std::string, std::string> m_config_map;
std::string m_config_file;
};
#endif //!__CONFIG_FILE_READER_H__
//构造 解析
CConfigFileReader::CConfigFileReader(const char* filename)
{
loadFile(filename);
}
CConfigFileReader::~CConfigFileReader()
{
}
1、文件打开
void CConfigFileReader::loadFile(const char* filename)
{
m_config_file.clear();
m_config_file.append(filename);
FILE* fp = fopen(filename, "r");
if (!fp)
return;
char buf[256];
for (;;)
{
char* p = fgets(buf, 256, fp);//每行读取256
if (!p)
break;
size_t len = strlen(buf);
if (buf[len - 1] == '\n')
buf[len - 1] = 0; // remove \n at the end
char* ch = strchr(buf, '#'); // remove string start with #
if (ch)
*ch = 0;
if (strlen(buf) == 0)
continue;
parseLine(buf);//逐行分析
}
fclose(fp);
m_load_ok = true;
}
2、逐行分析
void CConfigFileReader::parseLine(char* line)
{
char* p = strchr(line, '=');//分隔符
if (p == NULL)
return;
*p = 0;
char* key = trimSpace(line);
char* value = trimSpace(p + 1);
if (key && value)
{
m_config_map.insert(std::make_pair(key, value));
}
}
3、分析一行的key/value
char* CConfigFileReader::trimSpace(char* name)
{
// remove starting space or tab
char* start_pos = name;
while ( (*start_pos == ' ') || (*start_pos == '\t') || (*start_pos == '\r'))
{
start_pos++;
}
if (strlen(start_pos) == 0)
return NULL;
// remove ending space or tab
char* end_pos = name + strlen(name) - 1;
while ( (*end_pos == ' ') || (*end_pos == '\t') || (*end_pos == '\r'))
{
*end_pos = 0;
end_pos--;
}
int len = (int)(end_pos - start_pos) + 1;
if (len <= 0)
return NULL;
return start_pos;
}
4、读写值
char* CConfigFileReader::getConfigName(const char* name)
{
if (!m_load_ok)
return NULL;
char* value = NULL;
std::map<std::string, std::string>::iterator it = m_config_map.find(name);
if (it != m_config_map.end())
{
value = (char*)it->second.c_str();
}
return value;
}
int CConfigFileReader::setConfigValue(const char* name, const char* value)
{
if(!m_load_ok)
return -1;
std::map<std::string, std::string>::iterator it = m_config_map.find(name);
if(it != m_config_map.end())
{
it->second = value;
}
else
{
m_config_map.insert(std::make_pair(name, value));
}
return writeFile();
}
5、写文件
int CConfigFileReader::writeFile(const char* filename)
{
FILE* fp = NULL;
if(filename == NULL)
{
fp = fopen(m_config_file.c_str(), "w");
}
else
{
fp = fopen(filename, "w");
}
if(fp == NULL)
{
return -1;
}
//把map内的数据全部写入文件
char szPaire[128];
std::map<std::string, std::string>::iterator it = m_config_map.begin();
for (; it != m_config_map.end(); it++)
{
memset(szPaire, 0, sizeof(szPaire));
snprintf(szPaire, sizeof(szPaire), "%s=%s\n", it->first.c_str(), it->second.c_str());
size_t ret = fwrite(szPaire, strlen(szPaire),1,fp);
if(ret != 1)
{
fclose(fp);
return -1;
}
}
fclose(fp);
return 0;
}
6、使用
CConfigFileReader config("etc/chatserver.conf");
const char* logbinarypackage = config.getConfigName("logbinarypackage");
2、json文件读写
1、读文件
bool readJsonCfg(const char* filename, Json::Value* pRoot)
{
Json::CharReaderBuilder builder;
string errs;
bool ret = false;
pRoot->clear();
ifstream ifs; //
ifs.open(filename);
builder["collectComments"] = false;
if (!parseFromStream(builder, ifs, pRoot, &errs)) // jsonRoot
{
printf("read file %s\n", errs.data());
ret = false;
}
else
{
// printf("parse object type\n");
ret = true;
}
ifs.close();
return ret;
}
2、写文件
bool writeJsonCfg(const char* filename,const char* content, int len)
{
FILE *f;
f = fopen(filename, "w");
if(!f)
{
cout << "file open error" << endl;
return false;
}
fwrite(content, len, 1, f);
fclose(f);
return true;
}
3、c++ 流文件读写
4、打印
void PrintHexArray(const uint8* pArray, uint16 iLen)
{
uint16 i = 0;
for(i = 0; i < iLen; i++)
{
printf("%02X ", (uint8)pArray[i]);
}
printf("\n");
}
void PrintASCIIArray(const uint8* pArray, uint16 iLen)
{
uint16 i = 0;
for(i = 0; i < iLen; i++)
{
printf("%c", (uint8)pArray[i]);
}
printf("\n");
}