使用C的GLib/GIO从Web上获取文件 [英] Fetch a file from web using GLib/GIO from C

查看:106
本文介绍了使用C的GLib/GIO从Web上获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该使用GLib/GIO库从Web上获取文件的功能是什么?

With what function should I fetch a file from the web using GLib/GIO libs?

如果我的文件来自:

gchar *path = "http://xxx.yyyServer/sharing/temp.txt"

我应该怎么下载?

对于本地文件,我只使用fopen和fread之类的C库.

For the local files I simply use C libraries like fopen and fread.

我应该如何做?

不幸的是,在教程中没有文件处理的示例.我只能从文件"对话框中看到文件选择器.

There is unfortunately no examples of file handling in the Tutorials. I can only see a File chooser from File Dialog boxes.

已使用注释中的工作代码进行了更新: 以下代码适用于大小未知的二进制文件.

UPDATED WITH WORKING CODE FROM COMMENTS: The code below works for binary files of unknown sizes.

char *name= http://127.0.0.1:8000/mybinfile


int getFile(char *name)
{

    GFile *f = g_file_new_for_uri(name);
    GFileInputStream *fis = NULL;
    GDataInputStream* dis = NULL;
    GError *err = NULL;
    //char buffer[2048];
    char *buffer;
    size_t length;
    int ret = -1;

    GFileInfo *info;

    int total_size = -1;

    /* get input stream */
    fis = g_file_read(f, NULL, &err);

    if (err != NULL) {
        fprintf(stderr, "ERROR: opening %s\n", name);
        g_object_unref(f);
        return -1;
    }

    info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (fis),G_FILE_ATTRIBUTE_STANDARD_SIZE,NULL, &err);
    if (info)
    {
        if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
            total_size = g_file_info_get_size (info);
            printf( "total_size = %d\n", total_size);
            g_object_unref (info);
    }

    // fill buffer
    if(total_size > 0){
        buffer = (char *) malloc(sizeof(char) * total_size);
        memset(buffer, 0, total_size);
        if ((length = g_input_stream_read (G_INPUT_STREAM(fis),
                    buffer, total_size, NULL, &err)) != -1) {
                printf( "reading file\n");
        }
        printf( "File length = %d\n", length);

            ret = 0;
        }
        // close streams
        g_object_unref(fis);
        g_object_unref(f);   
        return ret;
    }

推荐答案

HTTP是GIO支持的协议之一,因此,当您使用GIO函数而不是标准C函数时,您可以像打开任何其他文件一样打开HTTP URI.只需使用 g_file_new_for_uri 创建文件对象,然后您可以像本地文件一样阅读它.

HTTP is one of the protocols supported by GIO, so you can open an HTTP URI just like any other file when using the GIO functions instead of standard C functions. Just use g_file_new_for_uri to create the file object and then you can read it just like a local file.

您可以使用g_file_read获取给定URI的GFileInputStream,然后使用g_data_input_stream_new获取输入流的GDataInputStream,然后可以使用它逐行读取文件.您必须先将GFileInputStream上载到GInputStream,然后才能将其传递给g_data_input_stream_new(或在对其执行任何其他有用的操作之前),但是如果您正在C语言中编写GTK,那么您现在可能已经习惯了

You can use g_file_read to get a GFileInputStream for the given URI and then g_data_input_stream_new to get a GDataInputStream for the input stream, which you can then use to read the file line-by-line. You have to upcast the GFileInputStream to a GInputStream before you can pass it to g_data_input_stream_new (or before you can do anything else useful with it), but if you're programming GTK in C, you're probably used to that by now.

这篇关于使用C的GLib/GIO从Web上获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆