2013-01-24 72 views
1

我寫在GTK代碼中,打開一個文件,分析它作爲寫入修改文件

/*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); 

,並查看它在一個TreeView(在一個單獨的功能)。樹形視圖是:

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); 

其中cell_edited是寫在TreeView本身的數據功能:

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

問題是我也想更新文件中的數據。我可以得到一些關於如何做到這一點的幫助?

回答

1

您必須以您需要的相同格式構建文件的內容,然後寫出來。

你可以做到這一點的一種方法是使用GString來構建內容,然後使用g_file_set_contents寫出來。

// Create GString 
GString *str = g_string_new (""); 

// Iterate over the list store 
GtkTreeIter iter; 
gboolean valid; 

for (valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter); 
    valid; 
    valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter)) { 
    char *string0, *string1, *string2, *string3, *string4; 

    gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 
         0, &string0, 
         1, &string1, 
         2, &string2, 
         3, &string3, 
         4, &string4, 
         -1); 

    // I don't know the format of your file, so I'm just going to separate them with a comma 
    g_string_append_printf (str, "%s,%s,%s,%s,%s\n", string0, string1, string2, string3, string4); 

    // gtk_tree_model_get copies strings, so free them 
    g_free (string0); 
    g_free (string1); 
    g_free (string2); 
    g_free (string3); 
    g_free (string4); 
} 

// Set the file contents atomically, add error checking 
g_file_set_contents (filename, str->str, -1, NULL); 

// Free the GString 
g_string_free (str, TRUE); 
相關問題