zoukankan      html  css  js  c++  java
  • c/c++常用代码 -- ini文件操作

    #pragma once
    
    #include <string>
    #include <sstream>
    
    typedef std::basic_string<TCHAR>    tstring;
    
    
    class CIniCfg
    {
    public:
        CIniCfg()
        {
            TCHAR szTemp[MAX_PATH];
            GetModuleFileName(NULL, szTemp, sizeof(szTemp));
                    
            LPTSTR p = _tcsrchr(szTemp, _T('.'));
            if (p != NULL)
                _tcscpy(p, _T(".ini"));
            else
                _tcscat(szTemp, _T(".ini"));
            
            m_strFilePath = szTemp;
        }
            
        void SetName(LPCTSTR szName)
        {
            TCHAR szTemp[MAX_PATH];
            GetModuleFileName(NULL, szTemp, sizeof(szTemp));
                            
            LPTSTR p = _tcsrchr(szTemp, _T('\'));
            if (p != NULL)
            {
                _tcscpy(++p, szName);
            }        
            
            m_strFilePath = szTemp;
        }
        
        void SetPath(LPCTSTR szPath)
        {
            m_strFilePath = szPath;
        }
        
        BOOL SetInt(LPCTSTR lpAppName,  // pointer to section name
            LPCTSTR lpKeyName,  // pointer to key name
            int nValue)
        {
            std::basic_stringstream<TCHAR> ss;
            ss << nValue;    
            
            return WritePrivateProfileString(
                lpAppName,
                lpKeyName,
                ss.str().c_str(),    //strValue,
                m_strFilePath.c_str());
        }
        
        
        
        BOOL SetString(LPCTSTR lpAppName,  // pointer to section name
            LPCTSTR lpKeyName,  // pointer to key name
            LPCTSTR lpString)   // pointer to string to add
            
        {
            return WritePrivateProfileString(
                lpAppName,
                lpKeyName,
                lpString,
                m_strFilePath.c_str());
        }
        
        
        
        int GetInt(LPCTSTR lpAppName,                // address of section name
            LPCTSTR lpKeyName,                // address of key name
            int nDefault)                    // return value if key name is not found
        {
            return GetPrivateProfileInt(
                lpAppName,
                lpKeyName,  
                nDefault,      
                m_strFilePath.c_str());     
        }
        
        tstring GetString(LPCTSTR lpAppName,        // points to section name
            LPCTSTR lpKeyName,        // points to key name
            LPCTSTR lpDefault)        // points to default string
        {
            TCHAR szRet[MAX_PATH] = {0};
            DWORD dwSize = MAX_PATH;
            
            GetPrivateProfileString(
                lpAppName,            // points to section name
                lpKeyName,            // points to key name
                lpDefault,            // points to default string
                szRet,                // points to destination buffer
                dwSize,                // size of destination buffer
                m_strFilePath.c_str());
            
            return szRet;
        }
        
        
    protected:
        tstring        m_strFilePath;
    };
  • 相关阅读:
    leetcode 203
    vim插件管理器vundle
    centos7看电影
    getopt
    iOS/object-c: 枚举类型 enum,NS_ENUM,NS_OPTIONS
    "ALView+PureLayout.h"
    UIPageViewController教程
    (Mac ox 10.11+) CocoaPods安装,卸载,使用说明
    CocoaPods集成到Xcode项目中的步骤
    label_设置行距、字距及计算含有行间距的label高度
  • 原文地址:https://www.cnblogs.com/vc60er/p/4039093.html
Copyright © 2011-2022 走看看