用C++连接两个WCHAR_T数组

43.*_*4D. 1 c++ arrays string winapi lpcwstr

处理这些疯狂的字符串和数组令我头疼......

到目前为止,这是我的代码

wchar_t mypath[MAX_PATH];
wchar_t temppath[MAX_PATH];

GetModuleFileName(0, mypath, MAX_PATH);
GetTempPath(MAX_PATH, temppath);
CreateDirectory(???, NULL);
Run Code Online (Sandbox Code Playgroud)

前两个Windows API函数使用LPWSTR变量.第三个使用LPCWSTR.有什么主要区别?在我获得TEMP目录的路径后,我想在其中创建一个名为"test"的新目录.这意味着我需要将(L"test")追加到我的"temppath"变量中.有人可以给我一些关于如何使用这些数组的技巧.这就是让C++变得痛苦的原因.为什么不能每个人都只选择一种数据类型的字符串.wchar_t如何有用?这很难使用和操纵.

多谢你们!

hmj*_*mjd 5

前两个Windows API函数使用LPWSTR变量.第三个使用LPCWSTR.有什么主要区别?

LPCWSTR是以下const版本LPWSTR:

我想在其中创建一个名为"test"的新目录.这意味着我需要将(L"test")追加到我的"temppath"变量中.

使用std::wostringstream:

std::wostringstream wos;
wos << temppath  << L"\\test";
std::wstring fullpath(wos.str());
Run Code Online (Sandbox Code Playgroud)

或者只是一个std::wstring(如评论中的克里斯所建议的):

std::wstring fullpath(std::wstring(temppath) + L"\\test");
Run Code Online (Sandbox Code Playgroud)

生成连接版本.然后c_str()用作参数CreateDirectory():

if (CreateDirectory(fullpath.c_str(), NULL) ||
    ERROR_ALREADY_EXISTS == GetLastError())
{
    // Directory created or already existed.
}
else
{
     // Failed to create directory.
}
Run Code Online (Sandbox Code Playgroud)