C++ 为 CreateDirectory 将“const char*”转换为“LPCTSTR”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14370982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:16:56  来源:igfitidea点击:

Converting 'const char*' to 'LPCTSTR' for CreateDirectory

c++c2664

提问by ProGirlXOXO

#include "stdafx.h"
#include <string>
#include <windows.h>
using namespace std;

int main()
{
    string FilePath = "C:\Documents and Settings\whatever";
    CreateDirectory(FilePath, NULL);
return 0;
}

Error: error C2664: 'CreateDirectory' : cannot convert parameter 1 from 'const char *' to 'LPCTSTR'

错误:错误 C2664:“CreateDirectory”:无法将参数 1 从“const char *”转换为“LPCTSTR”

  1. How do I make this conversion?
  2. The next step is to set today's date as a string or char and concatenate it with the filepath. Will this change how I do step 1?
  3. I am terrible at data types and conversions, is there a good explanation for 5 year olds out there?
  1. 我如何进行这种转换?
  2. 下一步是将今天的日期设置为字符串或字符,并将其与文件路径连接起来。这会改变我做第 1 步的方式吗?
  3. 我在数据类型和转换方面很糟糕,对 5 岁的孩子有很好的解释吗?

采纳答案by Remy Lebeau

std::stringis a class that holds char-based data. To pass a std::stringdata to API functions, you have to use its c_str()method to get a char*pointer to the string's actual data.

std::string是一个包含char基于数据的类。要将std::string数据传递给 API 函数,您必须使用其c_str()方法来获取char*指向字符串实际数据的指针。

CreateDirectory()takes a TCHAR*as input. If UNICODEis defined, TCHARmaps to wchar_t, otherwise it maps to charinstead. If you need to stick with std::stringbut do not want to make your code UNICODE-aware, then use CreateDirectoryA()instead, eg:

CreateDirectory()将 aTCHAR*作为输入。如果UNICODE已定义,则TCHAR映射到wchar_t,否则映射到char。如果您需要坚持std::string但不想让您的代码UNICODE感知,请CreateDirectoryA()改用,例如:

#include "stdafx.h"
#include <string>
#include <windows.h>

int main()
{
    std::string FilePath = "C:\Documents and Settings\whatever";
    CreateDirectoryA(FilePath.c_str(), NULL);
    return 0;
}

To make this code TCHAR-aware, you can do this instead:

要使此代码TCHAR感知,您可以改为执行以下操作:

#include "stdafx.h"
#include <string>
#include <windows.h>

int main()
{
    std::basic_string<TCHAR> FilePath = TEXT("C:\Documents and Settings\whatever");
    CreateDirectory(FilePath.c_str(), NULL);
    return 0;
}

However, Ansi-based OS versions are long dead, everything is Unicode nowadays. TCHARshould not be used in new code anymore:

然而,基于 Ansi 的操作系统版本早已死了,现在一切都是 Unicode。 TCHAR不应再在新代码中使用:

#include "stdafx.h"
#include <string>
#include <windows.h>

int main()
{
    std::wstring FilePath = L"C:\Documents and Settings\whatever";
    CreateDirectoryW(FilePath.c_str(), NULL);
    return 0;
}

回答by Timo Geusch

If you're not building a Unicode executable, calling c_str()on the std::string will result in a const char*(aka non-Unicode LPCTSTR) that you can pass into CreateDirectory().

如果您没有构建 Unicode 可执行文件,则调用c_str()std::string 将产生一个const char*(又名非 Unicode LPCTSTR),您可以将其传递给CreateDirectory()。

The code would look like this:

代码如下所示:

CreateDirectory(FilePath.c_str(), NULL):

Please note that this will result in a compile error if you're trying to build a Unicode executable.

请注意,如果您尝试构建 Unicode 可执行文件,这将导致编译错误。

If you have to append to FilePathI would recommend that you either continue to use std::stringor use Microsoft's CStringto do the string manipulation as that's less painful that doing it the C way and juggling raw char*. Personally I would use std::stringunless you are already in an MFC application that uses CString.

如果您必须附加到FilePath我建议您继续使用std::string或使用 MicrosoftCString来进行字符串操作,因为这比使用 C 方式进行操作并处理原始字符 * 的痛苦要小。我个人会使用,std::string除非您已经在使用 CString 的 MFC 应用程序中。