5

我取得了很大的进展,但我有两个问题已经让我放慢了好几天。最大的是我想保存FindFileData.cFileName为字符串,但我不能!有什么帮助吗?

4

2 回答 2

2

我从这里复制了这个:如何将 wstring 转换为字符串? 它将 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]);
}
于 2013-04-25T11:40:41.660 回答
1

WIN32_FIND_DATA参考页cFileName是类型TCHAR[]。如果启用 UNICODE(TCHARwchar_t)使用std::wstring

#include <string>
std::wstring ws(FindFileData.cFileName);

否则使用std::string(原样TCHARchar

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>
于 2013-04-24T15:01:59.657 回答