I continue to use the same C++ libraries for COTS software that we use in my company. Their help files aren't great and when I send queries directly to the company it can be weeks or possibly months before I get a response. This, I think, is a simple problem that I'm hoping anyone who is handy at c++ (ie not me) can have a stab at answering.

I want to get a list of files in a directory that have the file extension 'ext'. I'm pretty sure that I want to use their library function dir_list() with a filter that only include files that have if the last 4 characters as ".ext". However, from their comments, I can't work out how to define this filter function or use it in dir_list(). Can anyone give me some hints, please? Yes, I can work around this, but it would be nice to use what is in the toolbox.

// Function to act as a filter to a directory list.
// This function must return True if a member of the list
// has past the criterion, and False if a member is to
// be excluded from the list due to failure to pass the
// filter criteria.
typedef bool (*filter_func)(const char[], const char[]);

void dir_list(
   StringList &file_list,   // List of files (returned)
   const char dir[],        // Directory to scan
   filter_func filter = NULL    // Filter function
);

Yes, use filesystem library from boost to get a list of file for a directory then search files with the required ending by using simple std:string.

Thank you cppgangster. Thing is, I would love to do it in 'one line' as it were. That is to get the list of files for the directory that has already been filtered.

So I'd like to know how to define filter_func and use it in dir_list().

That way I don't have to search through the list of files. I have no other reason to search through the list because I actually just want to know if there is at least one file with the 'ext' extension.

If you are on *nix you can just can opendir() function with path "*.ext". If it succeeds then you know that at least one file exists with that exteision. You can do the same on MS-Windows by calling FindFirstFile(). But if you want portablility between the two operating systems then use boost as previously suggested.

Edited 4 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.