I have this function :
static
void ReadFilesInDirectory(const std::string &target)
{
GDir *dir;
GError *error;
const gchar *filename;
gchar* pathPlusFilename;
dir = g_dir_open(target.c_str(), 0, &error);
while ((filename = g_dir_read_name(dir)))
{
pathPlusFilename = g_build_filename(target.c_str(),filename,(gchar*)NULL);
if( g_file_test(pathPlusFilename ,G_FILE_TEST_IS_REGULAR) )
{
std::cout << "(file) " << pathPlusFilename << std::endl;
}
else
{
if( g_file_test(pathPlusFilename ,G_FILE_TEST_IS_DIR) )
{
std::cout << "(directory) " << pathPlusFilename << std::endl;
chdir(filename);
ReadFilesInDirectory(pathPlusFilename);
}
}
}
if(error) g_error_free(error); // gives an error at running time
g_free(pathPlusFilename);
g_dir_close(dir);
}
there is a GError variable, but I don't know how to print something from a GError, http://sourcecodebrowser.com/glib2.0/2.25.8/gerror_8h.html . Running just that code trough a simple call from main like,
ReadFilesInDirectory("/home/user/pictures")
lead to an error at running time.
The Error Reporting section in the GLib Reference Manual explains it. Basically:
GError *error = nullptr; //!\\
dir = g_dir_open(target.c_str(), 0, &error);
if( error != NULL )
{
std::cout << error->message << std::endl;
g_clear_error (&error);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With