有没有类似Delphi的IncludeTrailingPathDelimiter()
的东西函数将 '\'
字符添加到路径字符串的末尾,而无需在我的 .cpp
文件中写入文字 "\\"
,或我项目的任何其他地方?
#include <shlobj.h>
TCHAR szFolderPath[MAX_PATH];
if (SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_LOCAL_APPDATA, FALSE))
{
cout << szFolderPath << endl;
}
最佳答案
在 Windows 上,有 Shell API 的 PathCchAddBackslash()
函数(使用起来比 PathAddBackslash()
更安全)。
#include <shlobj.h>
#include <Pathcch.h>
TCHAR szFolderPath[MAX_PATH];
if (SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_LOCAL_APPDATA, FALSE))
{
PathCchAddBackslash(szFolderPath, MAX_PATH);
cout << szFolderPath << endl;
}
否则,用 C++ 自己实现应该不难。
关于c++ - C++ 中的 IncludeTrailingPathDelimiter() 怎么样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49310032/