DWORD GetPrivateProfileString(
LPCTSTR lpAppName, //配置文件的section名
LPCTSTR lpKeyName, //配置文件的key名
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
[NETWORK]
ServerIP=100.100.100.53
[TOTAL]
MEM=100
HEX=0xFF
程序:
CFile iniNetFile;
char ip[16];
char str[320];
unsigned int memory = 0;
unsigned int hex_val = 0;
TCHAR localDir[216];
CString tempDir;
GetCurrentDirectory(1024, &localDir[0]);
printf("localDir = %s\n", localDir);
DWORD num=0;
tempDir = localDir;
CString path = tempDir + _T("\\Server.ini");
if (iniNetFile.Open(path, CFile::modeRead | CFile::shareDenyWrite) != TRUE)
{
printf("Server.ini is not exist\n");
return -1;
}
num=GetPrivateProfileString(_T("NETWORK"),_T("ServerIP"),_T(""), (LPTSTR)&ip[0],sizeof(ip), path);
printf("ip = %s\n", ip);
GetPrivateProfileString(_T("TOTAL"),_T("MEM"),_T(""), (LPTSTR)&str[0],sizeof(str), path);
memory = strtoul(&str[0], NULL, 0);
printf("memory = %d\n", memory);
GetPrivateProfileString(_T("TOTAL"),_T("HEX"),_T(""), (LPTSTR)&str[0],sizeof(str), path);
hex_val = strtoul(&str[0], NULL, 16);
printf("hex_val = 0x%x\n", hex_val);
GetPrivateProfileString(_T("TOTAL"),_T("FILE"),_T(""), (LPTSTR)&str[0],sizeof(str), path);
printf("str = %s\n", str);
CString filea;
if (PathIsRelative(str))
{
filea = str;
printf("filea = %s\n", filea);
}
else
{
filea = PathFindFileName(path);
printf("fileb = %s\n", filea);
}
iniNetFile.Close();
return 0;