不能转换FindFileData.将文件名转换为字符串
cannot convert FindFileData.cFileName to string
我正在取得很大的进展,但我有两个问题已经拖了我几天了。最大的是,我想保存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
参考页面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>
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数