Directory Listing

Started by
4 comments, last by Ajare 19 years, 5 months ago
I'm trying to find a way to get a directory listing in C/C++. I can't find anything, even in MSDN, but it must be possible. Basically, something that returns a list of pointers to file handles, or something. TIA.
Advertisement
FindFirstFile, FindFirstFileEx, FindNextFile

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Look at FindFirstFile()
- onebeer
Ah i posted this a while back on a related thread, you might be interested.

#define WIN32_LEAN_AND_MEAN//the define below is important you must have it#define _WIN32_WINNT 0x0400#include <windows.h>#include <cstdlib>#include <string>#include <list>#include <algorithm>#include <iterator>#include <iostream>bool enumerate_dir(const std::string& dir, std::list<std::string>&);int main(int argc, char *argv[]) {     if(argc == 1) {      std::cerr << argv[0] << " error usage: " << argv[0] << " <name-of-directory>\n";      return EXIT_FAILURE;   }   std::list<std::string> dir_list;   if(enumerate_dir(argv[1], dir_list))      std::copy(dir_list.begin(),                dir_list.end(),                std::ostream_iterator<std::string>(std::cout, ", "));   else      std::cerr << "No such Directory, please try again..";   return EXIT_SUCCESS;}bool enumerate_dir(const std::string& dir, std::list<std::string>& l) {   WIN32_FIND_DATA FindFileData;   HANDLE hFind;   hFind = FindFirstFileEx(dir.c_str(), FindExInfoStandard, &FindFileData,                 FindExSearchNameMatch, 0, 0);  if(hFind == INVALID_HANDLE_VALUE)     return false;   FindNextFile(hFind, &FindFileData);   while(GetLastError() != ERROR_NO_MORE_FILES) {      FindNextFile(hFind, &FindFileData);      l.push_back(FindFileData.cFileName);   }   return true;}


if you need something cross-platform you might wont to check out boost::filesystem
If you want to be more portable, use boost's filesystem sublibrary

e:f,b
Thanks, guys.

This topic is closed to new replies.

Advertisement