How to get list of files in a directory programmatically

后端 未结 2 1992
情深已故
情深已故 2020-12-22 10:03

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 u

相关标签:
2条回答
  • 2020-12-22 10:32

    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.

    0 讨论(0)
  • 2020-12-22 10:35

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

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

    0 讨论(0)
提交回复
热议问题