1

I need to open a file to read the contents and display its contents on the screen. This should be done using GIO file handling. I am going through the tutorial but as a practice, I need the code using GIO for the following c code. In c the program could be:

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

thanks in advance.

6
  • What is your problem? What have you tried to solve it? What do you mean with "print on terminal"? Does it mean "not within a GUI"? And where should GTK come into the game? File handling is not related to GUI framework. Commented Nov 30, 2016 at 10:05
  • i should write it using GIO. G_FIle handling should be done instead of normal file handling operations. Commented Nov 30, 2016 at 10:12
  • I should print the contents on screen. Commented Nov 30, 2016 at 10:14
  • Then if you want GIO, what is the connection to GTK? And what is your specific problem? Are you asking for a tutorial where to start from beginning? Commented Nov 30, 2016 at 10:17
  • I am going through the tutorial. But I could not understand totally as I am a student and I know only C. So, I wanted to try with this program. So, If anyone can provide it, It will be helpful. Commented Nov 30, 2016 at 10:36

2 Answers 2

5

This is a rough approximation of the exact behavior you currently have. It could be improved with error messages, reading a line at a time, etc.

#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; } 
Sign up to request clarification or add additional context in comments.

4 Comments

These are the errors that I am getting. error: expected expression before ‘GFile’ g_autoptr(GFile) file = g_file_new_for_path("locations.txt"); error: expected ‘;’ before ‘file’ g_autoptr(GFile) file = g_file_new_for_path("locations.txt"); error: expected expression before ‘GFileInputStream’ g_autoptr(GFileInputStream) in = g_file_read(file, NULL, NULL); error: expected ‘;’ before ‘in’ g_autoptr(GFileInputStream) in = g_file_read(file, NULL, NULL) ^ error: ‘in’ undeclared (first use in this function) if(!in)
You must have a rather old version of GLib. You can just remove the g_autoptr() stuff but you have to call g_object_unref() manually then.
Even if I remove g_autoptr() and use g_object_unref() the errors remain the same. How can I modify the code to use #include<gtk/gtk.h> instead of #include<gio/gio.h>
I am getting output when I change it to g_object_unref. After displaying the result, I am getting an error: error in './a.out' : free(): invalid pointer: 0xbfa30f74 aborted (core dumped)
0

The answer to my question:

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

1 Comment

This just a copy of tingpings answer without g_autoptr (and with unneeded gtk include) right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.