I am writing a code in gtk, that opens a file, parses it as
/*Open and scan the file*/
fd = open (filename, O_RDWR);
scanner = g_scanner_new (NULL);
g_scanner_input_file (scanner, fd);
....
....
g_scanner_destroy (scanner);
gtk_label_set_text(GTK_LABEL(flabel), filename);
close (fd);
and view it in a treeview(in a separate function). The treeview is as:
store = gtk_list_store_new (NUM_COLS, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_STRING);
tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
/* #1: KEY COLUMN */
cell = gtk_cell_renderer_text_new ();
g_object_set (cell,
"editable", TRUE,
NULL);
g_signal_connect (cell,
"edited",G_CALLBACK(cell_edited),
tree);
g_object_set_data (G_OBJECT (cell),
"column", GINT_TO_POINTER (COL_BIB_KEY));
GtkTreeViewColumn *col_key,*col_year,*col_type,*col_auth,*col_pub;
col_key=gtk_tree_view_column_new_with_attributes (
"Key", cell,
"text", COL_BIB_KEY,
NULL);
gtk_tree_view_column_set_sort_column_id( col_key, ID_KEY);
gtk_tree_view_append_column (GTK_TREE_VIEW (tree), col_key);
gtk_tree_view_column_set_max_width (col_key,100);
where the cell_edited is a function to write the data in the treeview itself:
void cell_edited(GtkCellRendererText *renderer,
gchar *path,
gchar *new_text,
GtkTreeView *treeview)
{
guint column;
GtkTreeIter iter;
GtkTreeModel *model;
gpointer columnptr = g_object_get_data(G_OBJECT(renderer), "column");
column = GPOINTER_TO_UINT(columnptr);
if (g_ascii_strcasecmp (new_text, "") != 0)
{
model = gtk_tree_view_get_model (treeview);
if (gtk_tree_model_get_iter_from_string (model, &iter, path))
gtk_list_store_set (GTK_LIST_STORE (model), &iter, column, new_text, -1);
}
}
The problem is I also want to update the data in the file. Can I get some help on how to do that?
You’ll have to build the contents of the file in the same format that you need, then write it out.
One way you could do it is using GString to build the contents and then use g_file_set_contents to write it out.