0

I have searched everything , but no source codes i found work with VS C++ 2008,
Do you have any way to find list of files in a directory programmatically?

I am using VS 2008 C++ on Windows.

roalz
  • 2,699
  • 3
  • 25
  • 42
Ata
  • 12,126
  • 19
  • 63
  • 97
  • 1
    please show what you have tried and explain what doesn't work – Mat Jun 12 '11 at 06:44
  • 1
    Have you really tried? This is the first result google will give you http://www.cplusplus.com/forum/beginner/9173/ – nacho4d Jun 12 '11 at 06:45
  • 1
    You might want to look at both the question and answers at: http://stackoverflow.com/q/2531874/179910 (he edited code into the question, so it has a possible answer as well...) – Jerry Coffin Jun 12 '11 at 06:46
  • 1
    all of this code nee dirent.h and it is not in windows ! – Ata Jun 12 '11 at 06:49

2 Answers2

6

This shall find the list of files in C: drive, It doesn't use dirent.h just simple file handling api's,
FindFirstFile & FindNextFile

#include <windows.h>

int main(int argc, char* argv[])
{
   WIN32_FIND_DATA search_data;

   memset(&search_data, 0, sizeof(WIN32_FIND_DATA));

   HANDLE handle = FindFirstFile("c:\\*", &search_data);

   while(handle != INVALID_HANDLE_VALUE)
   {
      cout<<"\n"<<search_data.cFileName;

      if(FindNextFile(handle, &search_data) == FALSE)
        break;
   }

   //Close the handle after use or memory/resource leak
   FindClose(handle);
   return 0;
}

You should have a look at the standard api's on the msdn website.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
4

If you are using Boost, then you can use boost::filesystem.

If you are using Qt, then you can use QDir.

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
  • +1 for boost.filesystem. That one is almost part of the C++ standard and everyone wants it in there, so look for it in the next TR... in the meantime, that fine boost library will save you a ton of headache and writing. Give it a try. – Kerrek SB Jun 12 '11 at 09:43