#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
system("pause");
return;
}
else
{
do
{
printf("%s\n",FindFileData.cFileName);
}
while (FindNextFile(hFind,&FindFileData)!=0);
FindClose(hFind);
}
system("pause");
FindClose(hFind);
}
I need to get a folder list in output, but it gives me the following:
.
.
f
f
f
Actually, my folder listing is:
.
..
file1
file2
file3
Why do i have only first letter of file name? Thanks.
Use _tprintf(TEXT("%s\n"), FindFileData.cFileName)
.
In your case FindFileData.cFileName
is of actual type wchar_t
, so with printf
you are printing wide character string as if it were ascii.
You're passing a TCHAR*
to a function expecting a char*
. If you're compiling with TCHAR
as wchar_t
, every other byte in the string will be 0, so printf
will see every other byte as a terminating null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With