3

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.

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 1
    `GLib` and `glibc` are two completely different libraries. Which one are you talking about? – Mat Oct 09 '11 at 14:25
  • @Mat Sorry. Hate to admit it, but I'm an idiot. – ApprenticeHacker Oct 09 '11 at 14:28
  • 1
    GLIb is the name the GTK people chose for their library of utility functions and should not be confused with the GNU glibc library. There is some documentation at http://developer.gnome.org/glib/2.29/glib-File-Utilities.html. As e.dan points out, there are C(++) functions. The advantage to GLib is that GLIb generally does its own memory allocations where C(++) doesn't. – user732933 Oct 09 '11 at 15:07

3 Answers3

10

If you are looking for a glib example, here you go.

GDir *dir;
GError *error;
const gchar *filename;

dir = g_dir_open(".", 0, &error);
while ((filename = g_dir_read_name(dir)))
    printf("%s\n", filename);
cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

On POSIX systems, yes, I believe opendir/readdir/closedir and the associated dirent structure are what you need. On Win32, not so sure, but maybe this helps?

Community
  • 1
  • 1
e.dan
  • 7,275
  • 1
  • 26
  • 29
0

Nowadays you use Gio for this. Here is an example from GNOME Shell, using the Javascript binding, gjs. Something like:

    const directory = GLib.build_filenamev([ 'some', 'path']);

    try {
        fileEnum = directory.enumerate_children('standard::name,standard::type',
                                                Gio.FileQueryInfoFlags.NONE, null);
    } catch (e) {
        fileEnum = null;
    }
    if (fileEnum != null) {
        let info;
        while ((info = fileEnum.next_file(null))) {
            // Do something with info
            // or with fileEnum.get_child(info)
        }
    }
manuq
  • 182
  • 1
  • 5