tags:

views:

92

answers:

3

Hello. I have this code:

#include "stdafx.h"
#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.

+3  A: 

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.

Joe Gauterin
And what i have to do?
Ax
+7  A: 

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.

Constantin
Thanks a lot to all
Ax
+1  A: 

Use _tprintf or wprintf instead of printf.

Philipp