Im trying to learn glib/gtk. I wrote little code which prints files in directory and assigns “f” if they are normal files or “d” if they are directory. Problem is with if. It always gets false value and appends “f” to file name.
#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);
}
}
}
g_dir_read_namereturns just the directory/file name. You need to build full path in order to test it usingg_file_test. You can useg_build_filenamefor that.