不能转换FindFileData.将文件名转换为字符串

cannot convert FindFileData.cFileName to string

本文关键字:转换 字符串 文件名 FindFileData 不能      更新时间:2023-10-16

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

我从这里复制了这个:如何将wstring转换为字符串?它直接将wstring转换为string(包括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]);
}

来自WIN32_FIND_DATA参考页面cFileNameTCHAR[]类型。如果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>