用 Windows API 创建和编辑 .ini 文件

1. 与 .ini 文件相关的 API 有两类:

1)操作系统配置文件 Win.ini 的函数

GetProfileSection
GetProfileString
GetProfileInt
WriteProfileSection
WriteProfileString
2)操作用户自定义配置文件 PrivateProfile.ini 的函数

GetPrivateProfileSectionNames
GetPrivateProfileSection
GetPrivateProfileString
GetPrivateProfileInt
GetPrivateProfileStruct
WritePrivateProfileSection
WritePrivateProfileString
WritePrivateProfileStruct
2. 操作用户自定义配置文件相关 API 的示例代码

#include 
#include 
#include 
#pragma comment(lib, "strsafe.lib")
 
#define INI_FILE_NAME _T("ini_test.ini")
 
TCHAR gIniFileFullPath[MAX_PATH] = {_T('\0')};
 
int main()
{
    DWORD dwRet = 0;
    BOOL  bRet  = FALSE;
 
    // 获取 ini 文件全路径
    dwRet = ::GetModuleFileName(NULL, gIniFileFullPath, MAX_PATH);
    if (0UL == dwRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling GetModuleFileName, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
    if (MAX_PATH == dwRet && ERROR_INSUFFICIENT_BUFFER == ::GetLastError())
    {
        _ftprintf(stderr, _T("Error: The buffer is too small to hold the module name.\n"));
        return -1;
    }
 
    _tprintf(_T("The full path for the current module is: \n\t%s\n"), gIniFileFullPath);
 
    DWORD dwLoopIdx = dwRet - 1; 
    while (gIniFileFullPath[dwLoopIdx] != _T('\\'))
    {
        --dwLoopIdx;
    }
    ::StringCchCopy(gIniFileFullPath + (dwLoopIdx + 1), MAX_PATH - (dwLoopIdx + 1), INI_FILE_NAME);
    _tprintf(_T("The full path for %s is: \n\t%s\n"), INI_FILE_NAME, gIniFileFullPath);
    
    // ---------------------------------------------- WritePrivateProfileSection
    // 注: 如果 gIniFileFullPath 表示的 .ini 文件不存在, 会先创建一个.
    TCHAR szInsertedKeyValuePair[1024] = {_T('\0')};
 
#if defined(_UNICODE) || defined(UNICODE)
    wchar_t* dest  = NULL;
    wchar_t* src   = NULL;
    size_t   count = 0;
 
    dest  = szInsertedKeyValuePair;
    src   = L"Key11=defaultValue11";
    count = wcslen(L"Key11=defaultValue11");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"Key12=defaultValue12";
    count = wcslen(L"Key12=defaultValue12");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"Key13=defaultValue13";
    count = wcslen(L"Key13=defaultValue13");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
#else
    char*  dest  = NULL;
    char*  src   = NULL;
    size_t count = 0;
 
    dest  = szInsertedKeyValuePair;
    src   = "Key11=defaultValue11";
    count = strlen("Key11=defaultValue11");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "Key12=defaultValue12";
    count = strlen("Key12=defaultValue12");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "Key13=defaultValue13";
    count = strlen("Key13=defaultValue13");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
#endif // defined(_UNICODE) || defined(UNICODE)
 
    bRet = ::WritePrivateProfileSection(_T("Section1"), szInsertedKeyValuePair, gIniFileFullPath);
    if (FALSE == bRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
    // ---------------------------------------------- WritePrivateProfileSection
 
    // ----------------------------------------------- WritePrivateProfileString
    TCHAR szStr4Int[10] = {_T('\0')};
    _itot(-77, szStr4Int, 10);
    bRet = ::WritePrivateProfileString(_T("Section1"), _T("Key12"), szStr4Int, gIniFileFullPath);
    if (FALSE == bRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileString, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
 
    bRet = ::WritePrivateProfileString(_T("Section1"), _T("Key13"), _T("Value13"), gIniFileFullPath);
    if (FALSE == bRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileString, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
    // ----------------------------------------------- WritePrivateProfileString
 
    // ------------------------------------------- GetPrivateProfileSectionNames
    TCHAR szReturnBuffer[1024] = {_T('\0')};
    dwRet = ::GetPrivateProfileSectionNames(szReturnBuffer, 1024, gIniFileFullPath);
    if (1024 - 2 == dwRet)
    {
        _ftprintf(stderr, _T("Error: szReturnBuffer is not large enough to ")
                          _T("contain all the section names associated with ")
                          _T("the specified initialization file.\n"));
        return -1;
    }
    // ------------------------------------------- GetPrivateProfileSectionNames
 
    // ------------------------------------------------ GetPrivateProfileSection
    // 在 Section1 末尾追加 Key14=defaultValue14
    dwRet = ::GetPrivateProfileSection(_T("Section1"), szReturnBuffer, 1024, gIniFileFullPath);
    if (1024 - 2 == dwRet)
    {
        _ftprintf(stderr, _T("Error: szReturnBuffer is not large enough to ")
                          _T("contain all the key name and value pairs ")
                          _T("associated with Section1.\n"));
        return -1;
    }
 
#if defined(_UNICODE) || defined(UNICODE)
    dest  = szReturnBuffer + dwRet;
    src   = L"Key14=DefaultValue14";
    count = wcslen(L"Key14=DefaultValue14");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
#else
    dest  = szReturnBuffer + dwRet;
    src   = "Key14=DefaultValue14";
    count = strlen("Key14=DefaultValue14");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
#endif // defined(_UNICODE) || defined(UNICODE)
 
    bRet = ::WritePrivateProfileSection(_T("Section1"), szReturnBuffer, gIniFileFullPath);
    if (FALSE == bRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
    // ------------------------------------------------ GetPrivateProfileSection
 
    // ---------------------------------------------- WritePrivateProfileSection
    // 在 .ini 文件末尾追加 Section2 
    ZeroMemory(static_cast(szInsertedKeyValuePair), sizeof(szInsertedKeyValuePair));
 
#if defined(_UNICODE) || defined(UNICODE)
    dest  = szInsertedKeyValuePair;
    src   = L"Key21=defaultValue21";
    count = wcslen(L"Key21=defaultValue21");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"Key22=defaultValue22";
    count = wcslen(L"Key22=defaultValue22");
    wmemcpy(dest, src, count);
 
    dest += count;
    src   = L"\0";
    count = 1;
    wmemcpy(dest, src, count);
#else
    dest  = szInsertedKeyValuePair;
    src   = "Key21=defaultValue21";
    count = strlen("Key21=defaultValue21");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "Key22=defaultValue22";
    count = strlen("Key22=defaultValue22");
    memcpy(static_cast(dest), static_cast(src), count);
 
    dest += count;
    src   = "\0";
    count = 1;
    memcpy(static_cast(dest), static_cast(src), count);
#endif // defined(_UNICODE) || defined(UNICODE)
 
    bRet = ::WritePrivateProfileSection(_T("Section2"), szInsertedKeyValuePair, gIniFileFullPath);
    if (FALSE == bRet)
    {
        _ftprintf(stderr, _T("Error: Error occurs in calling WritePrivateProfileSection, ")
                          _T("error code is %lu.\n"), 
                          ::GetLastError());
        return -1;
    }
    // ---------------------------------------------- WritePrivateProfileSection
 
    // ---------------------------------------------------- GetPrivateProfileInt
    // 注: MSDN 中, 该函数原型的返回值数据类型为 UINT, 但, 并非是说它只能返回
    //     无符号整型值. 通过该函数, .ini 文件中的负数是可以正确取得的.
    int nIntValue4Key12 = ::GetPrivateProfileInt(_T("Section1"), _T("Key12"), -1, gIniFileFullPath);
    _tprintf(_T("\nnIntValue4Key12 = %i\n"), nIntValue4Key12);
    // ---------------------------------------------------- GetPrivateProfileInt
 
    // ------------------------------------------------- GetPrivateProfileString
    // Read value for Key11
    dwRet = ::GetPrivateProfileString(_T("Section1"), 
                                      _T("Key11"), 
                                      _T("Key11 key cannot be found in the initialization file."), 
                                      szReturnBuffer, 
                                      MAX_PATH, 
                                      gIniFileFullPath);
    if (MAX_PATH - 1 == dwRet)
    { 
        _ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
                          _T("and the supplied destination buffer is too small ")
                          _T("to hold the requested string.\n"));
        return -1;
    }
    if (MAX_PATH - 2 == dwRet)
    {
        _ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
                          _T("the supplied destination buffer is too small to ")
                          _T("hold all the strings.\n"));
        return -1;
    }
    _tprintf(_T("\nValue for Key11 is %s.\n"), szReturnBuffer);
    
    // Read value for Key12
    dwRet = ::GetPrivateProfileString(_T("Section1"), 
                                      _T("Key12"), 
                                      _T("Key12 key cannot be found in the initialization file."), 
                                      szReturnBuffer, 
                                      MAX_PATH, 
                                      gIniFileFullPath);
    if (MAX_PATH - 1 == dwRet)
    { 
        _ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
                          _T("and the supplied destination buffer is too small ")
                          _T("to hold the requested string.\n"));
        return -1;
    }
    if (MAX_PATH - 2 == dwRet)
    {
        _ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
                          _T("the supplied destination buffer is too small to ")
                          _T("hold all the strings.\n"));
        return -1;
    }
    _tprintf(_T("Value for Key12 is %s.\n"), szReturnBuffer);
    
    // Read value for Key13
    dwRet = ::GetPrivateProfileString(_T("Section1"), 
                                      _T("Key13"), 
                                      _T("Key13 key cannot be found in the initialization file."), 
                                      szReturnBuffer, 
                                      MAX_PATH, 
                                      gIniFileFullPath);
    if (MAX_PATH - 1 == dwRet)
    { 
        _ftprintf(stderr, _T("Error: Neither lpAppName nor lpKeyName is NULL ")
                          _T("and the supplied destination buffer is too small ")
                          _T("to hold the requested string.\n"));
        return -1;
    }
    if (MAX_PATH - 2 == dwRet)
    {
        _ftprintf(stderr, _T("Error: Either lpAppName or lpKeyName is NULL and ")
                          _T("the supplied destination buffer is too small to ")
                          _T("hold all the strings.\n"));
        return -1;
    }
    _tprintf(_T("Value for Key13 is %s.\n\n"), szReturnBuffer);
    // ------------------------------------------------- GetPrivateProfileString
 
    return 0; 
}
    
参考:

1. MSDN 在线文档

2. VC操作INI文件

你可能感兴趣的:(Windows,C/C++)