WIN32_FIND_DATA FileNAme doesn't put out the filenames but instead put's out HEx-Codes [duplicate]

限于喜欢 提交于 2019-12-14 03:25:19

问题


I tried to get a full list of all the files in a folder like this:

#include<Windows.h>
#include<iostream>
#include<stdio.h>

using namespace std;

void main()
{
HANDLE dateiHandle;
WIN32_FIND_DATA wfindD;

dateiHandle = FindFirstFile(L"E:\\Roman\\PIC\\funpics\\*", &wfindD);
do
{
    cout << wfindD.cFileName << endl;
} while (FindNextFile(dateiHandle, &wfindD));

FindClose(dateiHandle);
while (1)
{

}
}

and I can't figure out why the results are like this:

00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC
00AFFCCC

回答1:


TCHAR will be typedefed to wchar_t if you have unicode support enabled in your project (the default recent versions of Visual Studio). std::cout doesn't have any special handling for a wchar_t* and falls back on the void* overload for operator<<, which just prints the memory address pointed to as a hex number. Use std::wcout instead, which does have an operator<< overload for wchar_t*, and will print the strings like you expect.

As a side note, you'll have fewer surprises if you always explicitly use the A (for ANSI) or W (for wide) names for Win32 functions and structures that handle strings. To support non-ascii strings, you're generally better off using the W versions. In this case, FindFirstFileW, FindNextFileW, and WIN32_FIND_DATAW. FindClose doesn't directly interact with strings, so there's no A or W version of it.




回答2:


Use std::wcout instead of std::cout and you'll see the correct names printed out. 1

Your app is compiled for Unicode, so you're really calling FindFirstFileW(), which modifies a WIN32_FIND_DATAW structure, whose cFileName member is type WCHAR[], which is a double-byte "wide" character string.

1 Although, if the file names really do have double-byte characters (over 255), such as Japanese, then you may need to tweak other settings in your Command Prompt to actually see the double-byte characters correctly.



来源:https://stackoverflow.com/questions/49203394/win32-find-data-filename-doesnt-put-out-the-filenames-but-instead-puts-out-hex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!