无法将FindFileData.cFileName转换为字符串
我正在取得巨大进展,但我有两个问题让我放慢了几天。最大的是我想节省FindFileData.cFileName
作为字符串,但我不能!任何帮助?无法将FindFileData.cFileName转换为字符串
从WIN32_FIND_DATA
参考页cFileName
是TCHAR[]
类型。如果启用UNICODE(TCHAR
是wchar_t
)使用std::wstring
:
#include <string>
std::wstring ws(FindFileData.cFileName);
以其它方式使用std::string
(如TCHAR
是char
):
std::string ws(FindFileData.cFileName);
或者,要同时迎合:
std::basic_string<TCHAR> s(FindFileData.cFileName);
// std::string is a typedef for std::basic_string<char>
// std::wstring is a typedef for std::basic_string<wchar_t>
我到了这一点,我知道如何做到这一点.....但是如何将wstring转换为字符串? – alwaystudent 2013-04-24 15:40:58
你为什么想这样做。使用'wstring'。 – 2013-04-24 16:34:57
,因为我总是想学到比我目前所知道的更多的东西! :-) – alwaystudent 2013-04-25 09:54:22
我从这里复制它:How to convert wstring into string? 它将ws直接转换为字符串(包括FindFileData.cFileName)。任何更好的建议或任何有用的评论?
#include <clocale>
#include <locale>
#include <string>
#include <vector>
inline std::string narrow(std::wstring const& text)
{
std::locale const loc("");
wchar_t const* from = text.c_str();
std::size_t const len = text.size();
std::vector<char> buffer(len + 1);
std::use_facet<std::ctype<wchar_t> >(loc).narrow(from, from + len, '_', &buffer[0]);
return std::string(&buffer[0], &buffer[len]);
}
发表一些代码!你尝试了什么? – 2013-04-24 15:00:39
我通常会这样做,但这次我在网上搜索可能的解决方案,我写了一些尝试,但他们看起来像垃圾,所以我删除了一切,并在这里问了问题。 – alwaystudent 2013-04-24 15:43:05