c++cfilesystemsglib

How do I use Glib (or Any other Library) to list all the files in a directory?


I cannot figure out how to use GLib (GIO?) to list all the files (file-names?) in a given directory. There is no good doc or tutorial. Any code snippets are welcome. If it is not possible (or too troublesome) to do this using GLib, Is there any good C or C++ third party library to do this. EXCEPT Boost.FileSystem as I have a lot of trouble compiling boost.filesystem on minGw. There is of course "dirent.h" as a last resort , but it isn't standard and although it is supported on gnu gcc (mingw) , it is not included in the MSVC toolchain. So is it recommended to use dirent.h? Any good solution welcome.

Note: I don't know whether this should be tagged as c or c++. I am using c++ but glib is a c library.


Solution

  • Here is a GLib example, using g_dir_open and g_dir_read_name:

    GDir *dir;
    GError *error;
    const gchar *filename;
    
    dir = g_dir_open(".", 0, &error);
    while ((filename = g_dir_read_name(dir)))
        printf("%s\n", filename);