【问题标题】:glib, how to handle GError stuffs?glib,如何处理 GError 的东西?
【发布时间】:2015-03-16 08:57:02
【问题描述】:

我有这个功能:

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

有一个 GError 变量,但我不知道如何从 GError http://sourcecodebrowser.com/glib2.0/2.25.8/gerror_8h.html 打印一些东西。通过来自 main 的简单调用运行该代码,例如,

 ReadFilesInDirectory("/home/user/pictures")

导致运行时出错。

【问题讨论】:

    标签: c++ linux glib


    【解决方案1】:

    Error Reporting section in the GLib Reference Manual 解释了它。基本上:

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

    【讨论】:

    • 您必须将g_clear_error (error); 更改为g_clear_error (&amp;error);。即便如此,我没有收到任何消息槽std::cout &lt;&lt; error-&gt;message &lt;&lt; std::endl;
    • 我发现了问题,来自GError *error;的声明我需要指向nullptr
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    相关资源
    最近更新 更多