如果直接读取本地ini配置文件读取不到,例如下面的代码
int N=GetPrivateProfileInt("CustomConfig","Count",0,"Config\\Custom.ini");
因为没有通过相对路径设置地址,所以是当前的环境是工程绝对位置,而文件的输出目录可能配置的不是当前工程的输出位置,就可能出错。
有两种解决方式:
1.获取exe所在位置,并将路径添加上当前路径。
CString GetExePath()
{
TCHAR szPath[MAX_PATH] = {0};
GetModuleFileName(NULL, szPath, MAX_PATH);
// 去掉文件名,只保留路径
CString strPath = szPath;
int pos = strPath.ReverseFind('\\');
if(pos > 0)
{
strPath = strPath.Left(pos);
}
return strPath;
}
string iniPath = GetExePath() + "Config\\Custom.ini";
int N=GetPrivateProfileInt("CustomConfig","Count",0,iniPath);
2.对调试路径做设置。
输出目录和调试的工作目录相同即可。