Reputation: 619
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);
}
}
}
Upvotes: 1
Views: 2725
Reputation: 5025
g_dir_read_name
returns just the directory/file name. You need to build full path in order to test it using g_file_test
. You can use g_build_filename
for that.
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 );
}
Upvotes: 3