This question already has an answer here:
-
How I can print the wchar_t values to console?
7 answers
-
cout a string gets the address instead of value [duplicate]
1 answer
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
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.
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.