【发布时间】:2018-01-01 05:18:53
【问题描述】:
我正在创建一个控制台程序,它将通过控制台从用户那里获取一个目录并将其存储在 wchar_t 变量中。
例如,这可能是用户的可能输入:
C:/Users/Skipher/Destop/test.txt
我找到了std::wcin,似乎在 wchar_t 变量中写了一些东西。但是,当我尝试使用std::wcout 再次将其打印到控制台时,它只会从整个目录中打印出C。
我是否错误地写入 wchar_t 变量?我看到 wchar_t 变量以L"some kind of string" 的形式保存值。如何确保我的值被正确写入?
std::string stringPath;
std::cin.ignore();
std::getline(std::cin, stringPath);
std::wstring wstrPath = std::wstring(stringPath.begin(), stringPath.end());
const wchar_t* wcharPath = wstrPath.c_str();
mFilePath = (pxcCHAR*)wcharPath;
在调试过程中,我看到 wstrPath 具有我想要的值:L"C:/Users/Skipher/Destop/test.txt
但是,wcharPath 的值为0xcccccccc <Error reading characters of string.>
最后,我需要将值存储到 pxcCHAR* 变量 mFilePath,它是 SDK 中提供的 wchar_t* 的 typedef。如何从std::string 到pxcCHAR* 进行这种长时间的转换?
【问题讨论】:
-
wchar_t类型的变量只能包含一个字符。您可能正在寻找std::wstring