• Advertisement
Sign in to follow this  

Directory Listing

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
FindFirstFile, FindFirstFileEx, FindNextFile

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement