开发者

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

开发者 https://www.devze.com 2023-04-11 14:33 出处:网络
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 trou

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.


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);


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?


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)
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号