https://blog.csdn.net/naibozhuan3744/article/details/78783446
https://blog.csdn.net/rayborn1105/article/details/8192142
https://blog.csdn.net/g5dsk/article/details/6860162
// ConsoleApplication1.cpp: 定義控制台應用程序的入口點。 // #include "stdafx.h" #include <iostream> #include <windows.h> //using namespace std; int main() { //std::string strName, strTemp; //int nAge; //strName = "張三"; //nAge = 12; //::WritePrivateProfileString(TEXT("StudentInfo"), TEXT("Name"), TEXT("張三"), TEXT(".\\student.ini")); // write WritePrivateProfileString(TEXT("LiMing"), TEXT("Sex"), TEXT("Man"), TEXT(".\\student.ini"));// 路徑realse WritePrivateProfileString(TEXT("LiMing"), TEXT("Age"), TEXT("20"), TEXT(".\\student.ini")); WritePrivateProfileString(TEXT("Fangfang"), TEXT("Sex"), TEXT("Woman"), TEXT(".\\student.ini")); WritePrivateProfileString(TEXT("Fangfang"), TEXT("Age"), TEXT("21"), TEXT(".\\student.ini")); // read LPTSTR LiMingSex = new TCHAR[6] ; int LiMingAge = 0; LPTSTR FangfangSex = new TCHAR[6];; int FangfangAge = 0; GetPrivateProfileString(TEXT("LiMing"), TEXT("Sex"), TEXT(""), LiMingSex, 6 ,TEXT(".\\student.ini")); LiMingAge = GetPrivateProfileInt(TEXT("LiMing"), TEXT("Age"), 0, TEXT(".\\student.ini")); char LM[6] = {0}; strncpy_s(LM, (char *)LiMingSex,3); system("pause"); return 0; }
[Section1] Key11=value11 Key12=value12 [Section2] Key21=value21 Key22=value22 ... [SectionN] KeyN1=valueN1 KeyN2=valueN2
(1)操作系統配置文件Win.ini的函數:
函數名 |
功能 |
GetProfileSection |
讀取win.ini中指定節lpAppName中所有鍵名及其值。lpReturnedString字符串形式如下: Key1=Value1/0Key2=Value2/0…KeyN=ValueN/0/0 |
GetProfileString |
讀取win.ini中指定節lpAppName中鍵名為lpKeyName對應變量的字符串值。 |
GetProfileInt |
讀取win.ini中指定節lpAppName中鍵名為lpKeyName對應變量的整數值。 |
|
|
WriteProfileSection |
寫(替換)win.ini中指定節lpAppName中的鍵值。 lpString字符串形式同GetProfileSection中的lpReturnedString。 |
WriteProfileString |
寫(替換)win.ini中指定節lpAppName中鍵名為lpKeyName對應變量的字符串值。 |
(2)操作用戶自定義配置文件(PrivateProfile.ini)的函數:
函數名 |
功能 |
GetPrivateProfileSectionNames |
讀取lpFileName指定的配置文件中所有的節名。lpszReturnBuffer字符串形式如下: Section1/0Section2/0…SectionN/0/0 |
GetPrivateProfileSection |
同GetProfileSection。 |
GetPrivateProfileString |
同GetProfileString。 |
GetPrivateProfileInt |
同GetProfileInt |
GetPrivateProfileStruct |
須同WritePrivateProfileStruct配套使用。 |
|
|
WritePrivateProfileSection |
同WriteProfileSection |
WritePrivateProfileString |
同WriteProfileString |
WritePrivateProfileStruct |
不常用。 |
用MFC配置ini文件的核心函數有三個,分別為WritePrivateProfileString和GetPrivateProfileString和GetPrivateProfileInt。下面分別講解這三個核心函數的功能和各個參數的意義。
WritePrivateProfileString函數
BOOL WritePrivateProfileString( LPCTSTRlpAppName, LPCTSTRlpKeyName, LPCTSTRlpString, LPCTSTRlpFileName ); //其中各參數的意義: LPCTSTR lpAppName; //是INI文件中的一個字段名. LPCTSTR lpKeyName;//是lpAppName下的一個鍵名,通俗講就是變量名. LPCTSTR lpString; //是鍵值, 也就是變量的值,不過必須為LPCTSTR型或CString型的. LPCTSTR lpFileName;//是完整的INI文件名.
WritePrivateProfileString("StudentInfo1","身份證","44022520070001",".\\ConfigFile\\ConfigInit.ini");
[StudentInfo] Name = 李四 Age = 18 身份證 = 44022520070309 [StudentInfo1] Name = 李四 Age = 18 身份證 = 44022520070309
GetPrivateProfileString函數
DWORD GetPrivateProfileString( LPCTSTRlpAppName, LPCTSTRlpKeyName, LPCTSTRlpDefault, LPTSTRlpReturnedString, DWORDnSize, LPCTSTRlpFileName ); //其中各參數的意義: //前二個參數與 WritePrivateProfileString中的意義一樣. lpAppName; //是INI文件中的一個字段名. lpKeyName;// 是lpAppName下的一個鍵名,通俗講就是變量名. lpDefault;// : 如果INI文件中沒有前兩個參數指定的字段名或鍵名,則將此值賦給變量. lpReturnedString;// : 接收INI文件中的值的CString對象,即目的緩存器. nSize;// : 目的緩存器的大小. lpFileName;// : 是完整的INI文件名.
GetPrivateProfileInt函數
UINT GetPrivateProfileInt( LPCTSTRlpAppName, LPCTSTRlpKeyName, INTnDefault, LPCTSTRlpFileName );//其中各參數的意義: //前二個參數與 WritePrivateProfileString中的意義一樣. LPCTSTR lpAppName; //是INI文件中的一個字段名. LPCTSTR lpKeyName;//是lpAppName下的一個鍵名,通俗講就是變量名. INT nDefault; //如果INI文件中沒有前兩個參數指定的字段名或鍵名,則將此值賦給變量. LPCTSTR lpFileName;//是完整的INI文件名.
調用 GetPrivateProfileInt("StudentInfo2","Age", 1,".\\ConfigFile\\ConfigInit.ini");
#include <Windows.h> #include <tchar.h> #include <strsafe.h> #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<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "Key12=defaultValue12"; count = strlen("Key12=defaultValue12"); memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "Key13=defaultValue13"; count = strlen("Key13=defaultValue13"); memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(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<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(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<void*>(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<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "Key22=defaultValue22"; count = strlen("Key22=defaultValue22"); memcpy(static_cast<void*>(dest), static_cast<void*>(src), count); dest += count; src = "\0"; count = 1; memcpy(static_cast<void*>(dest), static_cast<void*>(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;