【问题标题】:How is IncludeTrailingPathDelimiter() in C++?C++ 中的 IncludeTrailingPathDelimiter() 是如何实现的?
【发布时间】:2018-08-24 21:19:31
【问题描述】:

是否有类似于 Delphi 的 IncludeTrailingPathDelimiter() 函数将 '\' 字符添加到路径字符串的末尾,而无需在我的 .cpp 文件或我项目的任何其他位置写入文字 "\\"

#include <shlobj.h>

TCHAR szFolderPath[MAX_PATH];

if (SHGetSpecialFolderPath(NULL, szFolderPath, CSIDL_LOCAL_APPDATA, FALSE))
{
    cout << szFolderPath << endl;

}

【问题讨论】:

  • 你可以使用 c++ 17 吗?
  • @CaptainGiraffe,我正在使用 VS 2013
  • 将此代码称为 C++ 令人反感

标签: c++ visual-c++ visual-studio-2013


【解决方案1】:

在 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++ 自己实现这一点应该不难。

【讨论】:

    猜你喜欢
    • 2011-03-05
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多