Ata
Ata

Reputation: 12544

How to get list of files in a directory programmatically

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.

Upvotes: 0

Views: 18882

Answers (2)

Johan Råde
Johan Råde

Reputation: 21357

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

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

Upvotes: 4

Alok Save
Alok Save

Reputation: 206498

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.

Upvotes: 6

Related Questions