本文介绍了为CreateDirectory将'const char *'转换为'LPCTSTR'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

错误:error C2664:'CreateDirectory':无法将参数1从'const char *'to'LPCTSTR'


  1. 如何进行此转换?

  2. 是将今天的日期设置为字符串或字符,并将其与文件路径连接。这将改变我如何做第1步?

  3. 我对数据类型和转换很可怕,5岁的人有很好的解释吗?


解决方案

std :: string 是一个保存 char 的数据的类。要将 std :: string 数据传递给API函数,必须使用 c_str()方法 char * 指向字符串实际数据的指针。



CreateDirectory $ c>接受 TCHAR * 作为输入。如果定义 UNICODE TCHAR 映射到 wchar_t 它映射到 char 。如果你需要坚持使用 std :: string ,但是不想让你的代码 UNICODE CreateDirectoryA(),例如:

  #includestdafx.h 
#include< string>
#include< windows.h>

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

要使此代码 TCHAR

  #includestdafx.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;但是,基于Ansi的操作系统版本已经死了,现在的一切都是Unicode。 TCHAR 不应在新代码中使用:

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

int main()
{
std :: wstring FilePath = LC:\\Documents and Settings\\whatever;
createDirectoryW(FilePath.c_str(),NULL);
return 0;
}


#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'

  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?
解决方案

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

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

#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:

#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. TCHAR should not be used in new code anymore:

#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;
}

这篇关于为CreateDirectory将'const char *'转换为'LPCTSTR'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 23:59