Réduire
Brouillon
0
04/05/2013
0
Récupérer la liste des noms de fichier d'un répertoire
Mots-clés:
Microsoft Visual C++
#include <windows.h>
#include <string.h>
#include <stdio.h>

int listFiles(char *dir)
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = INVALID_HANDLE_VALUE;
   char DirSpec[MAX_PATH];  // directory specification
   DWORD dwError;

   printf ("Target directory is %s.\n", dir);
   strncpy (DirSpec, dir, strlen(dir)+1);
   strncat (DirSpec, "\\*", 3);

   hFind = FindFirstFile(DirSpec, &FindFileData);

   if (hFind == INVALID_HANDLE_VALUE) 
   {
      printf ("Invalid file handle. Error is %u\n", GetLastError());
      return (-1);
   } 
   else 
   {
      printf ("First file name is %s\n", FindFileData.cFileName);
      while (FindNextFile(hFind, &FindFileData) != 0) 
      {
         if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            printf ("Next file name is %s\n", FindFileData.cFileName);
      }

      dwError = GetLastError();
      FindClose(hFind);
      if (dwError != ERROR_NO_MORE_FILES) 
      {
         printf ("FindNextFile error. Error is %u\n", dwError);
         return (-1);
      }
   }
   return 0;
}

int main(int argc, char *argv[])
{
   return listFiles("c:\\");
}