加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – FindFirstFile的预期输入类型是什么?

发布时间:2020-12-16 09:46:05 所属栏目:百科 来源:网络整理
导读:我首先要说的是我基本上不了解宽字符串和Unicode支持.我让QString和QFile在99%的时间内为我处理这个问题,但我正在尝试编译为VC6编写的其他人的库. 当我在Qt Creator中使用MSVC2010进行编译时,出现此错误: error: C2664: 'FindFirstFileW' : cannot convert
我首先要说的是我基本上不了解宽字符串和Unicode支持.我让QString和QFile在99%的时间内为我处理这个问题,但我正在尝试编译为VC6编写的其他人的库.

当我在Qt Creator中使用MSVC2010进行编译时,出现此错误:

error: C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast,C-style cast or function-style cast

代码使用的是FindFirstFile函数,该函数是重载的(取决于您是否使用Unicode字符集进行编译).当FindFirstFileA和FindFirstFileW的输入似乎是两种完全不同的类型时,我不明白FindFirstFile的类型是什么.

所以这是我的问题:FindFirstFile的预期输入类型是什么?

推论:我如何获取const char *类型的文件名并将其放入FindFirstType将接受的表单中?

解决方法

FindFirstFile是一个定义如下的宏:

#ifdef UNICODE
#define FindFirstFile  FindFirstFileW
#else
#define FindFirstFile  FindFirstFileA
#endif // !UNICODE

这意味着它在使用UNICODE定义编译时扩展为具有W的那个,并且它扩展为具有A的那个.

现在,FindFirstFile的第一个参数是LPCSTR或LPWCSTR. LPCSTR是const char *的typedef,而LPWCSTR是const wchar_t *的typedef.在您的错误消息中,您尝试将一种类型的const char *作为第一个参数传递给FindFirstFileW,它接受类型为const wchar_t *的参数,因此出错.

为了匹配类型,你需要传递一个const wchar_t *类型的对象,你有几个选择:

std::wstring path1 = L"..."; // 1
const wchar_t* path2 = L"..."; // 2
wchar_t path3[] = L"..."; // 3

WIN32_FIND_DATA  w32fd;
FindFirstFile(path1.c_str(),&w32fd); // 1
FindFirstFile(path2,&w32fd); // 2
FindFirstFile(path3,&w32fd); // 3
FindFirstFile(L"...",&w32fd);

How do I take a filename of type const char* and put it into a form
that FindFirstType will accept?

如果您的文件名只包含基本ASCII字符集中的字符,那么您可以将其转换为std :: wstring,如下所示:std :: wstring path(std :: begin(filename),std :: end(filename)) ;.否则,您需要使用MultiByteToWideChar或many of the options shown here.另一种选择是直接调用FindFirstFileA,但如果您使用的是Windows,通常最好使用wchar_t开头.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读