码迷,mamicode.com
首页 > 编程语言 > 详细

c/c++常用代码 -- ini文件操作

时间:2014-10-21 00:43:30      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   sp   文件   div   

#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;
};

 

c/c++常用代码 -- ini文件操作

标签:des   style   blog   color   io   ar   sp   文件   div   

原文地址:http://www.cnblogs.com/wansui/p/4039093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!