#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] = {};
        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文件操作的更多相关文章

  1. ini文件操作

    Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...

  2. ini 文件操作记要(1): 使用 TIniFile

    ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Class ...

  3. winform INI文件操作辅助类

    using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...

  4. [C#] 常用工具类——文件操作类

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

  5. Ini文件操作类

    /// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...

  6. ansible笔记(5):常用模块之文件操作(二)

    ansible笔记():常用模块之文件操作(二) 文件操作类模块 find模块 find模块可以帮助我们在远程主机中查找符合条件的文件,就像find命令一样. 此处我们介绍一些find模块的常用参数, ...

  7. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  8. C#读写ini文件操作

    ini文件,是windows操作系统下的配置文件,ini文件是一种按照特点方式排列的文本文件,它的构成分为三部分,结构如下: [Section1] key 1 = value2 key 1 = val ...

  9. C# Ini文件操作

    在开源中国看到的操作ini文件的,写的还不看,留着以后用 using System; using System.IO; using System.Runtime.InteropServices; us ...

随机推荐

  1. 【Struts 2】Struts2环境搭建

    一.关键步骤 1.创建Java Web项目 2.引入Struts2的依赖包,将依赖包拷贝到WEB-INF/lib下 * commons-logging-1.0.4.jar * freemarker-2 ...

  2. namespace的用法

    C++中采用的是单一的全局变量命名空间.在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突.当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字:有时为了程序 ...

  3. linux设置ulimit值永久生效

    小知识的积累,转自 http://hi.baidu.com/moonelf9989/blog/item/1deadf12780fa0c5c2fd789d.html linux 默认打开文件数linux ...

  4. use python get information from one page

    #!/usr/bin/python read = file('thread-1554-1-1.html','r') wr = file('list','w') while 1: line=read.r ...

  5. Count and Say [LeetCode 38]

    1- 问题描述 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211 ...

  6. 在Eclipse中查看JDK类库的源代码

    http://blog.csdn.net/a81895898/article/details/8486802 在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> ...

  7. C#winform初试报表

    步骤1.新建一个窗口,拖一个ReportViewer控件上去,设置该控件的Dock为Fill. 步骤2.添加一个报表文件,如:Report1.rdlc,设置其中的参数,这里设置的参数和下面的代码里面的 ...

  8. 小菜的系统框架界面设计-灰姑娘到白雪公主的蜕变(工具条OutLookBar)

    灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌   --蜕变-->  我这里讲的是一个工具条的蜕变过程, ...

  9. Web前端性能优化的9大问题

    1.请减少HTTP请求基本原理:在浏览器(客户端)和服务器发生通信时,就已经消耗了大量的时间,尤其是在网络情况比较糟糕的时候,这个问题尤其的突出.一个正常HTTP请求的流程简述:如在浏览器中输入&qu ...

  10. SQL Server自增长列插入指定值 -- SET IDENTITY_INSERT ON|OFF(转)

    想要将值插入到自动编号(或者说是标识列,IDENTITY)中去,需要设定 SET IDENTITY_INSERT 示例: 1.首先建立一个有标识列的表:CREATE TABLE products (i ...