Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将FindFileData.cFileName转换为字符串_C++_Winapi - Fatal编程技术网

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]);
}