ApprenticeHacker ApprenticeHacker -3 years ago 125
C++ Question

Can anyone demonstrate fl_filename_list?

I can't figure out how to use the fl_filename_list function in the

module. I also cannot find any good examples or tutorial on the internet. Can anyone provide a good example?

Answer Source

fl_filename_list() is nothing but a cross-platform wrapper around scandir() function. If you are familiar with that one, then you should easily use fl_filename_list() too.

// FILE: fl_filename_list.cpp
// RUN:  fltk-config --compile fl_filename_list.cpp && ./fl_filename_list

#include <FL/filename.H>
#include <iostream>

int main() {
  dirent** list;
  // by default we will use the fl_numericsort() function declared in
  // the filename.H . Here are others, and you can certainly
  // use the source to find out how to write your own ;)
/* code snippet: filename.H
00107 FL_EXPORT int fl_alphasort(struct dirent **, struct dirent **);
00108 FL_EXPORT int fl_casealphasort(struct dirent **, struct dirent **);
00109 FL_EXPORT int fl_casenumericsort(struct dirent **, struct dirent **);
00110 FL_EXPORT int fl_numericsort(struct dirent **, struct dirent **);
  int numOfFiles = fl_filename_list("/tmp", &list);
  // or, int numOfFiles = fl_filename_list("/tmp", &list, fl_alphasort);
  std::cout << "Number of files: " << numOfFiles << std::endl;
  for (int i = 0; i < numOfFiles; i++)
    std::cout << list[i]->d_name << std::endl;
  return 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download