环境:win7_64 + VS2012
程序test.exe,设置为开机启动。test.exe中会生成length.txt,xxx.log文件,但是仅指定了length.txt,xxx.log生成的相对路径。
假设test.exe所在目录,C:\\Program Files (x86)\\Test\\test.exe,以生成length.txt代码片段举例说明。
CString filePath = _T(\"Length.txt\");
CStdioFile file;
if(!file.Open(filePath,
CFile::modeWrite | CFile::modeCreate | CFile::osWriteThrough |CFile::shareDenyWrite, NULL))
{
SL_LOG_MSG(_T(\"文件length.txt打开失败!\"));
return;
};
期望length.txt生成路径为C:\\Program Files (x86)\\Test\\length.txt,实际开机启动时,length.txt生成路径为C:\\Windows\\SysWOW64\\length.txt
猜测可能原因:操作系统启动时环境变量path,有默认值C:\\Windows\\SysWOW64\\。系统把相对路径进行了拼接,最终路径成了C:\\Windows\\SysWOW64\\length.txt
修改方法,将相对路径换成绝对路径
CString filePath = _T(\"Length.txt\");
TCHAR cExecPath[MAX_PATH];
if(!GetModuleFileName( 0, cExecPath, sizeof(cExecPath)))
{
return ;
}
CString strExePath(cExecPath);
int nSeparator = strExePath.ReverseFind(\'\\\\\');
strExePath = strExePath.Left(nSeparator);
filePath.Format(_T(\"%s\\\\%s\"),strExePath,filePath);
CStdioFile file;
if(!file.Open(filePath,
CFile::modeWrite | CFile::modeCreate | CFile::osWriteThrough |CFile::shareDenyWrite, NULL))
{
SL_LOG_MSG(_T(\"文件Rodlength.txt打开失败!\"));
return;
};
验证可行。
继续阅读与本文标签相同的文章
上一篇 :
古巴期待与中企推进数字化转型
-
GSMA首席执行官洪曜庄:5G时代中国在引领
2026-05-14栏目: 教程
-
猎户星空CEO傅盛:现在是AI发展最好时期,家庭服务机器人前景可期
2026-05-14栏目: 教程
-
5G远程驾驶和微公交首秀互联网大会
2026-05-14栏目: 教程
-
学宏程序编程,这些知识必不可少!
2026-05-14栏目: 教程
-
华为准备卖出“落后”的5G,多家美企极力竞争!任正非格局太大!
2026-05-14栏目: 教程
