首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GIO处理文件

使用GIO处理文件
EN

Stack Overflow用户
提问于 2016-11-30 17:35:19
回答 2查看 1.4K关注 0票数 1

我需要打开一个文件来读取内容并将其内容显示在屏幕上。这应该使用GIO文件处理来完成。我正在学习本教程,但作为练习,我需要在以下c代码中使用GIO的代码。在c中,程序可以是:

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
int main()
{

  FILE *fp;
  char temp[1000];
  if(fp=fopen("locations.txt", "r") != NULL)
   {
     fgets(temp, 1000, fp);
     printf("%s", temp[1000]);
    }
 fclose(fp);
return 0;
}

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2016-12-01 08:28:51

这是您当前拥有的确切行为的粗略近似值。它可以通过错误消息、一次读取一行等进行改进。

代码语言:javascript
复制
#include <gio/gio.h>

int main(void)
{
    g_autoptr(GFile) file = g_file_new_for_path("locations.txt");
    g_autoptr(GFileInputStream) in = g_file_read(file, NULL, NULL);
    if(!in)
        return 1;

    gssize read;
    char temp[1000];

    while (TRUE)
    {
      read = g_input_stream_read(G_INPUT_STREAM(in), temp, G_N_ELEMENTS(temp) - 1, NULL, NULL);
      if (read > 0)
      {
          temp[read] = '\0';
          g_print("%s", temp);
      }
      else if (read < 0)
          return 1;
      else
         break;
    }

    return 0;
}
票数 4
EN

Stack Overflow用户

发布于 2016-12-01 18:10:40

我的问题的答案是:

代码语言:javascript
复制
#include <gtk/gtk.h>

int main(void)
{
GFile *file = g_file_new_for_path("FINAL_SERVER_URLS.txt");

GFileInputStream *in = g_file_read(file, NULL, NULL);
if(!in)
    return 1;

gssize read;
gchar temp[1000];

while (TRUE)
{
  read = g_input_stream_read(G_INPUT_STREAM(in), temp, G_N_ELEMENTS(temp) - 1, NULL, NULL);
  if (read > 0)
  {
      temp[read] = '\0';
      g_print("%s", temp);
  }
  else if (read < 0)
      return 1;
  else
     break;
}
//g_free(temp);
g_object_unref(file);
g_object_unref(in);
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40884738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档