C++ INI文件操作

C++ INI文件操作

INI文件結構:

由節名、鍵名、鍵值組成。形式如下:

[節名]

鍵名 = 鍵值

鍵名2 = 鍵值2

一個INI文件,可以有多個節

讀寫INI文件

  1. WritePrivateProfileString

    需要包含頭文件#include<Windows.h>

    該函數用於向INI文件中寫入一個字符串數據。

    函數原型:

    BOOL WritePrivateProfileString(
    	LPCTSTR lpAppName, //節名
        LPCTSTR lpKeyName, //鍵名
        LPCTSTR lpString, //鍵值
        LPCTSTR lpFileName, //保存的文件
    )
    
  2. GetPrivateProfileString

    該函數用於獲取INI文件中的鍵值。

    函數原型:

    DWORD GetPrivateProfileString(
    	LPCTSTR lpAppName, //節名
        LPCTSTR lpKeyName, //鍵名
        LPCTSTR lpDefault, //默認值
        LPTSTR lpReturnedString, //讀取的值
        DWORD nSize, //lpReturnedString緩衝區的大小
        LPCTSTR lpFileName, //INI文件名
    )
    

    返回值是字符複製到緩衝區的數量,不包括終止null字符。

  3. GetPrivateProfileInt

    該函數用於從INI文件中獲取整型數據。

    該函數原型:

    UINT GetPrivateProfileInt(
    	LPCTSTR lpAppName,//節名
        LPCTSTR lpKeyName, //鍵名
        INT nDefault, //默認值
        LPCTSTR lpFileName,//INI文件名
    )
    

    函數返回實際讀取的整數值

  4. GetPrivateProfileSectionNames

    該函數用於返回INI文件中的所有節名。

    函數原型:

    DWORD GetPrivateProfileSectionNames(
    	LPTSTR lpszReturnBuffer, //接收節名的數據緩衝區
        DWORD nSize, //緩衝區大小
        LPCTSTR lpFileName,//INI文件
    )
    

    返回值:

    返回值指定數量的字符複製到指定的緩衝,不包括終止null字符。

    如果緩衝區沒有達到足以包含所有相關的部分名稱指定的初始化文件,返回值等於指定的長度nSize - 2.

  5. GetPrivateProfileSection

    該函數用於獲取指定節下的所有的鍵名和鍵值。

    函數原型:

    DWORD GetPrivateProfileSection(
    	LPCTSTR lpAppName, //節名
        LPTSTR lpReturnedString, //接收數據緩衝區
        DWORD nSize, //緩衝區大小
        LPCTSTR lpFileName, //INI文件
    )
    

    示例程序如下:

    #include<Windows.h>
    void Test()
    {
        LPCTSTR lpFileName = TEXT("D:\\wo.ini");
        //寫入配置
        BOOL res = ::WritePrivateProfileString(TEXT("app"), TEXT("name"), TEXT("1"), lpFileName);
    
        //讀配置
        WCHAR content[100];
        DWORD v = ::GetPrivateProfileString(TEXT("app"), TEXT("name"), TEXT(""), content, 100, lpFileName);
    
        //讀數值
        DWORD resv = ::GetPrivateProfileInt(TEXT("app"), TEXT("name"), 0, lpFileName);
    
        //讀取所有節名
       
        resv == ::GetPrivateProfileSectionNames(content, 100, lpFileName);
    
        //讀取節下的所有鍵值
        resv = ::GetPrivateProfileSection(TEXT("app"), content, 100, lpFileName);
    
    }
    

    簡易封裝一下:

    #pragma once
    #include<Windows.h>
    
    class INIHelper
    {
    public:
    	INIHelper(LPCTSTR lpFileName)
    		:lpFileName(lpFileName)
    	{}
    	BOOL WriteKeyValue(LPCTSTR section, LPCTSTR key, LPCTSTR value)
    	{
    		return ::WritePrivateProfileString(section, key, value, this->lpFileName);
    	}
    	UINT ReadInt(LPCTSTR section, LPCTSTR key, int defaultvalue = 0)
    	{
    		return ::GetPrivateProfileInt(section, key, defaultvalue, this->lpFileName);
    	}
    	DWORD ReadSectionNames(LPWSTR buffer, int nsize)
    	{
    		return ::GetPrivateProfileSectionNames(buffer, nsize, this->lpFileName);
    	}
    	DWORD ReadSection(LPCTSTR section, LPWSTR buffer, int nsize)
    	{
    		return ::GetPrivateProfileSection(section, buffer, nsize, this->lpFileName);
    	}
    private:
    	LPCTSTR lpFileName;
    };
    
    
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章