1

glib/gtk を学ぼうとしています。ディレクトリ内のファイルを印刷し、通常のファイルの場合は「f」、ディレクトリの場合は「d」を割り当てるコードを少し書きました。問題はifにあります。常に偽の値を取得し、ファイル名に「f」を追加します。

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>

int main()
{
    GDir* home = NULL;
    GError* error = NULL;
    gchar* file = "a";

    home = g_dir_open("/home/stamp", 0, &error);
    while (file != NULL) 
    {
        file = g_dir_read_name(home);
        if (g_file_test(file, G_FILE_TEST_IS_DIR))
        {
            g_printf("%s: d\n", file);
        } else {
            g_printf("%s: f\n", file);
        }
    }
}
4

1 に答える 1

3

g_dir_read_nameディレクトリ/ファイル名だけを返します。を使用してテストするには、フル パスを作成する必要がありますg_file_test。そのために使えますg_build_filename

int main()
{
    GDir* home = NULL;
    GError* error = NULL;
    gchar* file = "a";

    home = g_dir_open("/home/stamp", 0, &error);
    while (file != NULL) 
    {
        file = g_dir_read_name(home);

        gchar* fileWithFullPath;
        fileWithFullPath = g_build_filename("/home/stamp", file, (gchar*)NULL);
        if (g_file_test(fileWithFullPath, G_FILE_TEST_IS_DIR))
        {
            g_printf("%s: d\n", file);
        }
        else
        {
            g_printf("%s: f\n", file);
        }
        g_free(fileWithFullPath);
    }
    g_dir_close( home );
}
于 2010-11-13T21:59:04.413 に答える