从网上获取文件:在GTK中使用C [英] Fetch a file from web: in GTK using C
问题描述
如果我的文件来自:
$ b $使用gtk库可以从网络获取文件的功能b
gchar * path =http://xxx.yyyServer/sharing/temp.txt
我应该怎么下载它?
对于本地文件,我只是简单地使用C库,如fopen和fread。
我应该如何进行上述操作?
>很遗憾,教程中没有文件处理的例子。我只能从文件对话框中看到一个文件选择器。
请帮助这个noob。
$ b
谢谢
p>使用注释工作代码进行更新:
以下代码适用于大小未知的二进制文件。
char * name = http://127.0.0.1:8000/mybinfile
$ b $ 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;
/ *获得输入流* /
fis = g_file_read(f,NULL,& err);
$ b $ if(err!= NULL){
fprintf(stderr,ERROR:opening%s\\\
,name);
g_object_unref(f);
返回-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 \\\
,total_size);
g_object_unref(info);
//填充缓冲区
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(读取文件\ n );
}
printf(文件长度=%d \ n,长度);
ret = 0;
}
//关闭流
g_object_unref(fis);
g_object_unref(f);
return ret;
}
HTTP是协议之一由GIO支持,所以在使用GIO功能而不是标准C函数时,您可以像打开任何其他文件一样打开HTTP URI。只需使用 g_file_new_for_uri
创建文件对象,然后您可以像读取本地文件一样读取它。
您可以使用 g_file_read
为给定URI获取一个GFileInputStream,然后为 g_data_input_stream_new
获取输入流的GDataInputStream,然后您可以使用该GDataInputStream逐行读取文件。在将它传递给 g_data_input_stream_new
之前(或者在您可以使用其他任何有用的东西之前),您必须将GFileInputStream转发到GInputStream,但是如果您要将GTK编程为C,你现在可能习惯了。
With what function should I fetch a file from the web using gtk libs?
If my file is from:
gchar *path = "http://xxx.yyyServer/sharing/temp.txt"
What should I do to download it?
For the local files I simply use C libraries like fopen and fread.
How should I do the above?
There is unfortunately no examples of file handling in the Tutorials. I can only see a File chooser from File Dialog boxes.
Please help this noob.
Thanks
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 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.
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.
这篇关于从网上获取文件:在GTK中使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!