首先介紹兩個(gè)函數(shù):
讀取 ini 文件:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // points to section name
LPCTSTR lpKeyName, // points to key name
LPCTSTR lpDefault, // points to default string
LPTSTR lpReturnedString, // points to destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // points to initialization filename
);

參數(shù)說(shuō)明:
lpAppName :ini 文件中的一個(gè)字段名
lpKeyName :lpAppName 下的一個(gè)鍵名,也就是具體的變量名
lpDefault :如果沒(méi)有其前兩個(gè)參數(shù)值,則將此值賦給變量
lpReturnedString :接收INI文件中的值的CString對(duì)象,即目的緩存器
nSize :目的緩存器的大小
lpFileName :完整的INI文件路徑名
寫(xiě)入ini 文件:
BOOL WritePrivateProfileString(
LPCTSTRlpAppName, // section name
LPCTSTRlpKeyName, // key name
LPCTSTRlpString, // string to add
LPCTSTRlpFileName // initialization file
);
參數(shù)說(shuō)明:
lpAppName :ini 文件中的一個(gè)字段名
lpKeyName :lpAppName 下的一個(gè)鍵名,也就是具體的變量名
lpString :是鍵值,也就是變量的值,必須為L(zhǎng)PCTSTR或CString類(lèi)型
lpFileName :完整的INI文件路徑名
讀取整型值:

UINT GetPrivateProfileInt(
LPCTSTRlpAppName, // section name
LPCTSTRlpKeyName, // key name
INTnDefault, // return value if key name not found
LPCTSTRlpFileName // initialization file name
);
實(shí)例:
寫(xiě)入:
CString StrName,Strtemp;
int nAge;
char filename[20] = "";
StrName = "jacky";
nAge = 13;

WritePrivateProfileString("Student","Name",StrName,"res\\setting.ini");

讀取:
CString SName;
GetPrivateProfileString("Student","Name","DefaultName",SName.GetBuffer(100),100,"res\\setting.ini");
SName.ReleaseBuffer();
MessageBox(SName);
