Note that there are some explanatory texts on larger screens.

plurals
  1. POc gtk+: loading a text file into a GtkSourceView's TextBuffer
    primarykey
    data
    text
    <p>I'm writing a program using the C language with gtk+ and gtksourceview-2.0.</p> <p>I'm using a GtkFileChooser for the user to choose a file and when he clicks on it, i want the content to be loaded to the GtkSourceView' TextBuffer</p> <p>this is the function that gets executed when a user double click's a file on the GtkFileChooser:</p> <pre><code>void on_file_activated(GtkWidget *widget, gpointer data) { GFile *file; FILE *fp; gchar *path_name; long file_size; gchararray file_buffer; file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(widget)); path_name=g_file_get_path(file); g_debug("%s is chosen\n", path_name); fp=fopen(path_name, "r"); g_assert( fp != NULL); fseek(fp, 0L, SEEK_END); file_size = ftell(fp); rewind(fp); g_debug("file size: %ld\n",file_size*sizeof(gchar)); file_buffer=calloc(file_size, sizeof(gchar)); g_assert(file_buffer != NULL); fread(&amp;file_buffer,file_size,1,fp); g_debug("after fread"); //file_buffer[file_size*sizeof(gchar)]=0; //g_debug("after adding zero: %s",file_buffer); gtk_text_buffer_set_text (textbuffer, file_buffer,2); g_debug("after set text"); g_object_unref(file); } </code></pre> <p>this is the output of my application:</p> <pre><code>** (tour_de_gtk:18107): DEBUG: /home/ufk/Projects/gtk-projects/tour-de-gtk/Debug/src/examples/example_gtk_label/main.c is chosen ** (tour_de_gtk:18107): DEBUG: file size: 16 ** (tour_de_gtk:18107): DEBUG: after fread </code></pre> <p>after then i get a segmentation fault on the command gtk_text_buffer_set_text</p> <p>as you can see i have two commands that are commented out. trying to g_debug the buffer which obviously creates a segmentation fault because i didn't add a zero to the end of the string, and even when I try to add zero to the end of the string i get a segmentation fault. I probably did something wrong.</p> <p>here i'm trying to write only the first two characters of the buffer but with no luck.</p> <p>any ideas?</p> <h1>update</h1> <p>the finished function:</p> <pre><code>void on_file_activated(GtkWidget *widget, gpointer data) { GFile *file; gchar *path_name; long file_size; gchar *file_buffer; GError *error; gboolean read_file_status; file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(widget)); path_name=g_file_get_path(file); g_debug("%s is chosen\n", path_name); read_file_status=g_file_get_contents (path_name,&amp;file_buffer,NULL, &amp;error); if (read_file_status == FALSE) { g_error("error opening file: %s\n",error &amp;&amp; error-&gt;message ? error-&gt;message : "No Detail"); return; } gtk_text_buffer_set_text (textbuffer, file_buffer,-1); g_debug("after set text"); g_object_unref(file); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload