分享

操作INI文件的读写类

 梦醉千秋 2016-09-10

http://www./Tips/376146/A-Small-Class-to-Read-INI-Files


IniReader.h

  1. //// Source: http://www./Articles/10809/A-Small-Class-to-Read-INI-File ////  
  2.   
  3. #ifndef INIREADER_H  
  4. #define INIREADER_H  
  5.   
  6. #include <windows.h>  
  7.   
  8. class CIniReader  
  9. {  
  10. public:  
  11.  CIniReader(LPCTSTR szFileName);   
  12.  int ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue);  
  13.  float ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue);  
  14.  bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue);  
  15.  LPTSTR ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue);  
  16. private:  
  17.   TCHAR m_szFileName[255];  
  18. };  
  19. #endif //INIREADER_H  

IniReader.cpp


  1. //// Source: http://www./Articles/10809/A-Small-Class-to-Read-INI-File ////  
  2.   
  3. #include "IniReader.h"  
  4. #include <iostream>  
  5. #include <windows.h>  
  6.   
  7. CIniReader::CIniReader(LPCTSTR szFileName)  
  8. {  
  9.  memset(m_szFileName, 0x00, sizeof(m_szFileName));  
  10.  memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));  
  11. }  
  12. int CIniReader::ReadInteger(LPCTSTR szSection, LPCTSTR szKey, int iDefaultValue)  
  13. {  
  14.  int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName);   
  15.  return iResult;  
  16. }  
  17. float CIniReader::ReadFloat(LPCTSTR szSection, LPCTSTR szKey, float fltDefaultValue)  
  18. {  
  19.  TCHAR szResult[255];  
  20.  TCHAR szDefault[255];  
  21.  float fltResult;  
  22.  _stprintf_s(szDefault, 255, TEXT("%f"),fltDefaultValue);  
  23.  GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 255, m_szFileName);   
  24.  fltResult =  (float)_tstof(szResult);  
  25.  return fltResult;  
  26. }  
  27. bool CIniReader::ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolDefaultValue)  
  28. {  
  29.  TCHAR szResult[255];  
  30.  TCHAR szDefault[255];  
  31.  bool bolResult;  
  32.  _stprintf_s(szDefault, 255, TEXT("%s"), bolDefaultValue? TEXT("True") : TEXT("False"));  
  33.  GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName);   
  34.  bolResult =  (_tcscmp(szResult, TEXT("True")) == 0 ||   
  35.         _tcscmp(szResult, TEXT("true")) == 0) ? true : false;  
  36.  return bolResult;  
  37. }  
  38. LPTSTR CIniReader::ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szDefaultValue)  
  39. {  
  40.  LPTSTR szResult = new TCHAR[255];  
  41.  memset(szResult, 0x00, sizeof(szResult));  
  42.  GetPrivateProfileString(szSection,  szKey, szDefaultValue, szResult, 255, m_szFileName);   
  43.  return szResult;  
  44. }  

IniWriter.h

  1. //// Source: http://www./Articles/10809/A-Small-Class-to-Read-INI-File ////  
  2.   
  3. #ifndef INIWRITER_H  
  4. #define INIWRITER_H  
  5.   
  6. #include <windows.h>  
  7.   
  8. class CIniWriter  
  9. {  
  10. public:  
  11.  CIniWriter(LPCTSTR szFileName);   
  12.  void WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue);  
  13.  void WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue);  
  14.  void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue);  
  15.  void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue);  
  16. private:  
  17.  TCHAR m_szFileName[255];  
  18. };  
  19. #endif //INIWRITER_H  

IniWriter.cpp


  1. //// Source: http://www./Articles/10809/A-Small-Class-to-Read-INI-File ////  
  2.   
  3. #include "IniWriter.h"  
  4. #include <iostream>  
  5. #include <windows.h>   
  6.   
  7. CIniWriter::CIniWriter(LPCTSTR szFileName)  
  8. {  
  9.  memset(m_szFileName, 0x00, sizeof(m_szFileName));  
  10.  memcpy(m_szFileName, szFileName, _tcslen(szFileName)*sizeof(TCHAR));  
  11. }  
  12. void CIniWriter::WriteInteger(LPCTSTR szSection, LPCTSTR szKey, int iValue)  
  13. {  
  14.  TCHAR szValue[255];  
  15.  _stprintf_s(szValue, 255, TEXT("%d"), iValue);  
  16.  WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);   
  17. }  
  18. void CIniWriter::WriteFloat(LPCTSTR szSection, LPCTSTR szKey, float fltValue)  
  19. {  
  20.  TCHAR szValue[255];  
  21.  _stprintf_s(szValue, 255, TEXT("%f"), fltValue);  
  22.  WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);   
  23. }  
  24. void CIniWriter::WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, bool bolValue)  
  25. {  
  26.  TCHAR szValue[255];  
  27.  _stprintf_s(szValue, 255, TEXT("%s"), bolValue ? TEXT("True") : TEXT("False"));  
  28.  WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);   
  29. }  
  30. void CIniWriter::WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szValue)  
  31. {  
  32.  WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);  
  33. }  

main.cpp

  1. #if defined(UNICODE) || defined(_UNICODE)  
  2. #define tcout std::wcout  
  3. #else  
  4. #define tcout std::cout  
  5. #endif  
  6.   
  7. #include <iostream>  
  8. #include <windows.h>  
  9. #include "IniWriter.h"  
  10. #include "IniReader.h"  
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14.  CIniWriter iniWriter(TEXT(".\\initest.ini"));  
  15.  iniWriter.WriteString(TEXT("Setting"), TEXT("Name"), TEXT("jianxx"));     
  16.  iniWriter.WriteInteger(TEXT("Setting"), TEXT("Age"), 27);   
  17.  iniWriter.WriteFloat(TEXT("Setting"), TEXT("Height"), 1.82f);   
  18.  iniWriter.WriteBoolean(TEXT("Setting"), TEXT("Marriage"), false);    
  19.  CIniReader iniReader(TEXT(".\\initest.ini"));  
  20.  LPTSTR szName = iniReader.ReadString(TEXT("Setting"), TEXT("Name"), TEXT(""));     
  21.  int iAge = iniReader.ReadInteger(TEXT("Setting"), TEXT("Age"), 25);   
  22.  float fltHieght = iniReader.ReadFloat(TEXT("Setting"), TEXT("Height"), 1.80f);   
  23.  bool bMarriage = iniReader.ReadBoolean(TEXT("Setting"), TEXT("Marriage"), true);   
  24.    
  25.  tcout<<"Name:"<<szName<<std::endl  
  26.    <<"Age:"<<iAge<<std::endl   
  27.    <<"Height:"<<fltHieght<<std::endl   
  28.    <<"Marriage:"<<bMarriage<<std::endl;   
  29.  delete szName;    
  30.  return 1;     
  31. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多