首先介紹兩個函數:
讀取 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
);

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

UINT GetPrivateProfileInt(
LPCTSTRlpAppName, // section name
LPCTSTRlpKeyName, // key name
INTnDefault, // return value if key name not found
LPCTSTRlpFileName // initialization file name
);
實例:
寫入:
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);
