C++ 无法将FindFileData.cFileName转换为字符串
我正在取得很大的进步,但我有两个问题已经让我慢了好几天。最大的问题是我想将C++ 无法将FindFileData.cFileName转换为字符串,c++,winapi,C++,Winapi,我正在取得很大的进步,但我有两个问题已经让我慢了好几天。最大的问题是我想将FindFileData.cFileName保存为字符串,但我不能!有什么帮助吗 参考页cFileName中的为类型。如果启用了UNICODE(TCHAR是wchar\t),请使用std::wstring: #include <string> std::wstring ws(FindFileData.cFileName); 或者,为了兼顾这两方面: std::basic_string<TCHAR>
FindFileData.cFileName
保存为字符串,但我不能!有什么帮助吗 参考页cFileName
中的为类型。如果启用了UNICODE(TCHAR
是wchar\t
),请使用std::wstring
:
#include <string>
std::wstring 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>
std::基本字符串(FindFileData.cFileName);
//std::string是std::basic_字符串的typedef
//std::wstring是std::basic_字符串的typedef
我从这里复制了这个:
它将wstring直接转换为字符串(包括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]);
}
#包括
#包括
#包括
#包括
内联标准::字符串窄(标准::wstring常量和文本)
{
std::locale const loc(“”);
wchar_t const*from=text.c_str();
std::size_t const len=text.size();
std::向量缓冲区(len+1);
std::使用facet(loc).窄(from,from+len,,',&buffer[0]);
返回std::string(&buffer[0],&buffer[len]);
}
发布一些代码!你尝试了什么?我通常是这样做的,但这次我在网上搜索可能的解决方案,我写了一些尝试,但它们看起来像垃圾,所以我删除了所有内容,并在这里提出了问题。我说到这一点,我知道如何做。。。。。但是如何将wstring转换为string呢?为什么要这样做呢。使用wstring
。因为我总是想学到比我现在知道的更多的东西!:-)<代码>字符串s(ws.begin(),ws.end())代码>
#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]);
}