获取当前文件路径:
函数原型:
DWORD GetCurrentDirectory(DWORD nBufferLength,LPWSTR lpBuffer)函数返回值:
返回0表示失败,当函数调用成功将返回写入缓冲区的字符串长度
缓冲区长度:
1、使用已定义的常量MAX_PATH(260);
2、使用给函数传入NULL的方法获得缓冲区长度。
例:
// TODO:加载配置文件
CString des=L"";
::GetCurrentDirectory(256,des.GetBuffer(256)); //获取当前文件路径
des.ReleaseBuffer();
des+=L"\\Camcfg.ini";
读取配置文件:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
LPCTSTR lpDefault, // 如果lpReturnedString为空,则把个变量赋给lpReturnedString
LPTSTR lpReturnedString, // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
DWORD nSize, // lpReturnedString的缓冲区大小
LPCTSTR lpFileName // INI文件的路径
);
例:
GetPrivateProfileString(L"CamSection",L"Name",L"0",m_name.GetBuffer(20),20,des);
GetPrivateProfileString(L"CamSection",L"Age",L"0",m_age.GetBuffer(20),20,des);
GetPrivateProfileString(L"CamSection",L"Hight",L"0",m_hight.GetBuffer(20),20,des);
GetPrivateProfileString(L"CamSection",L"Sex",L"0",m_sex.GetBuffer(20),20,des);
写入配置文件:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // INI文件中的一个字段名[节名]可以有很多个节名
LPCTSTR lpKeyName, // lpAppName 下的一个键名,也就是里面具体的变量名
LPCTSTR lpString, // 键值,也就是数据
LPCTSTR lpFileName // INI文件的路径
);
例:
WritePrivateProfileString(L"CamSection",L"Name",m_name.GetBuffer(m_name.GetLength()),des);
WritePrivateProfileString(L"CamSection",L"Age",m_age.GetBuffer(m_age.GetLength()),des);
WritePrivateProfileString(L"CamSection",L"Hight",m_hight.GetBuffer(m_hight.GetLength()),des);
WritePrivateProfileString(L"CamSection",L"Sex",m_sex.GetBuffer(m_sex.GetLength()),des);
文件内容: