1、普通文件读写

文件样式

  1. #client listener
  2. listenip=172.18.111.180
  3. listenport=20000
  4. #monitor listener
  5. monitorlistenip=0.0.0.0
  6. monitorlistenport=8888
  7. monitortoken=123
  8. #http listener
  9. httplistenip=0.0.0.0
  10. httplistenport=12345
  11. logfiledir=logs/
  12. logfilename=chatserver
  13. logbinarypackage=0
  14. #mysql config
  15. dbserver=172.18.111.180
  16. dbuser=root
  17. dbpassword=123456
  18. dbname=flamingo

0、头文件

  1. #ifndef __CONFIG_FILE_READER_H__
  2. #define __CONFIG_FILE_READER_H__
  3. #include <map>
  4. #include <string>
  5. class CConfigFileReader
  6. {
  7. public:
  8. CConfigFileReader(const char* filename);
  9. ~CConfigFileReader();
  10. char* getConfigName(const char* name);
  11. int setConfigValue(const char* name, const char* value);
  12. private:
  13. void loadFile(const char* filename);
  14. int writeFile(const char* filename = NULL);
  15. void parseLine(char* line);
  16. char* trimSpace(char* name);
  17. bool m_load_ok;
  18. std::map<std::string, std::string> m_config_map;
  19. std::string m_config_file;
  20. };
  21. #endif //!__CONFIG_FILE_READER_H__
  22. //构造 解析
  23. CConfigFileReader::CConfigFileReader(const char* filename)
  24. {
  25. loadFile(filename);
  26. }
  27. CConfigFileReader::~CConfigFileReader()
  28. {
  29. }

1、文件打开

  1. void CConfigFileReader::loadFile(const char* filename)
  2. {
  3. m_config_file.clear();
  4. m_config_file.append(filename);
  5. FILE* fp = fopen(filename, "r");
  6. if (!fp)
  7. return;
  8. char buf[256];
  9. for (;;)
  10. {
  11. char* p = fgets(buf, 256, fp);//每行读取256
  12. if (!p)
  13. break;
  14. size_t len = strlen(buf);
  15. if (buf[len - 1] == '\n')
  16. buf[len - 1] = 0; // remove \n at the end
  17. char* ch = strchr(buf, '#'); // remove string start with #
  18. if (ch)
  19. *ch = 0;
  20. if (strlen(buf) == 0)
  21. continue;
  22. parseLine(buf);//逐行分析
  23. }
  24. fclose(fp);
  25. m_load_ok = true;
  26. }

2、逐行分析

  1. void CConfigFileReader::parseLine(char* line)
  2. {
  3. char* p = strchr(line, '=');//分隔符
  4. if (p == NULL)
  5. return;
  6. *p = 0;
  7. char* key = trimSpace(line);
  8. char* value = trimSpace(p + 1);
  9. if (key && value)
  10. {
  11. m_config_map.insert(std::make_pair(key, value));
  12. }
  13. }

3、分析一行的key/value

  1. char* CConfigFileReader::trimSpace(char* name)
  2. {
  3. // remove starting space or tab
  4. char* start_pos = name;
  5. while ( (*start_pos == ' ') || (*start_pos == '\t') || (*start_pos == '\r'))
  6. {
  7. start_pos++;
  8. }
  9. if (strlen(start_pos) == 0)
  10. return NULL;
  11. // remove ending space or tab
  12. char* end_pos = name + strlen(name) - 1;
  13. while ( (*end_pos == ' ') || (*end_pos == '\t') || (*end_pos == '\r'))
  14. {
  15. *end_pos = 0;
  16. end_pos--;
  17. }
  18. int len = (int)(end_pos - start_pos) + 1;
  19. if (len <= 0)
  20. return NULL;
  21. return start_pos;
  22. }

4、读写值

  1. char* CConfigFileReader::getConfigName(const char* name)
  2. {
  3. if (!m_load_ok)
  4. return NULL;
  5. char* value = NULL;
  6. std::map<std::string, std::string>::iterator it = m_config_map.find(name);
  7. if (it != m_config_map.end())
  8. {
  9. value = (char*)it->second.c_str();
  10. }
  11. return value;
  12. }
  13. int CConfigFileReader::setConfigValue(const char* name, const char* value)
  14. {
  15. if(!m_load_ok)
  16. return -1;
  17. std::map<std::string, std::string>::iterator it = m_config_map.find(name);
  18. if(it != m_config_map.end())
  19. {
  20. it->second = value;
  21. }
  22. else
  23. {
  24. m_config_map.insert(std::make_pair(name, value));
  25. }
  26. return writeFile();
  27. }

5、写文件

  1. int CConfigFileReader::writeFile(const char* filename)
  2. {
  3. FILE* fp = NULL;
  4. if(filename == NULL)
  5. {
  6. fp = fopen(m_config_file.c_str(), "w");
  7. }
  8. else
  9. {
  10. fp = fopen(filename, "w");
  11. }
  12. if(fp == NULL)
  13. {
  14. return -1;
  15. }
  16. //把map内的数据全部写入文件
  17. char szPaire[128];
  18. std::map<std::string, std::string>::iterator it = m_config_map.begin();
  19. for (; it != m_config_map.end(); it++)
  20. {
  21. memset(szPaire, 0, sizeof(szPaire));
  22. snprintf(szPaire, sizeof(szPaire), "%s=%s\n", it->first.c_str(), it->second.c_str());
  23. size_t ret = fwrite(szPaire, strlen(szPaire),1,fp);
  24. if(ret != 1)
  25. {
  26. fclose(fp);
  27. return -1;
  28. }
  29. }
  30. fclose(fp);
  31. return 0;
  32. }

6、使用

  1. CConfigFileReader config("etc/chatserver.conf");
  2. const char* logbinarypackage = config.getConfigName("logbinarypackage");

2、json文件读写

1、读文件

  1. bool readJsonCfg(const char* filename, Json::Value* pRoot)
  2. {
  3. Json::CharReaderBuilder builder;
  4. string errs;
  5. bool ret = false;
  6. pRoot->clear();
  7. ifstream ifs; //
  8. ifs.open(filename);
  9. builder["collectComments"] = false;
  10. if (!parseFromStream(builder, ifs, pRoot, &errs)) // jsonRoot
  11. {
  12. printf("read file %s\n", errs.data());
  13. ret = false;
  14. }
  15. else
  16. {
  17. // printf("parse object type\n");
  18. ret = true;
  19. }
  20. ifs.close();
  21. return ret;
  22. }

2、写文件

  1. bool writeJsonCfg(const char* filename,const char* content, int len)
  2. {
  3. FILE *f;
  4. f = fopen(filename, "w");
  5. if(!f)
  6. {
  7. cout << "file open error" << endl;
  8. return false;
  9. }
  10. fwrite(content, len, 1, f);
  11. fclose(f);
  12. return true;
  13. }

3、c++ 流文件读写

4、打印

  1. void PrintHexArray(const uint8* pArray, uint16 iLen)
  2. {
  3. uint16 i = 0;
  4. for(i = 0; i < iLen; i++)
  5. {
  6. printf("%02X ", (uint8)pArray[i]);
  7. }
  8. printf("\n");
  9. }
  10. void PrintASCIIArray(const uint8* pArray, uint16 iLen)
  11. {
  12. uint16 i = 0;
  13. for(i = 0; i < iLen; i++)
  14. {
  15. printf("%c", (uint8)pArray[i]);
  16. }
  17. printf("\n");
  18. }