Select a file from the network: in GTK using C

What function should I extract a file from the Internet with gtk libs?

If my file is:

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

What to do to download it?

For local files, I just use C libraries like fopen and fread.

How do i do this?

Unfortunately, there are no examples of file processing in textbooks. I can only see file selections from the File dialogs.

Please help this noob.

thank

UPDATED WITH WORKING CODE FOR 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;
    }
+5
source share
2 answers

HTTP - , GIO, HTTP-URI, , GIO C. g_file_new_for_uri , .

g_file_read, GFileInputStream URI, g_data_input_stream_new, GDataInputStream , . GFileInputStream GInputStream, g_data_input_stream_new (, - ), GTK C, , , .

+11

noob, GTK (, ), , C/++. (wget ..) GET . C libcurl. !

-1

All Articles