如何将GTKTextBuffer的内容保存到文件 [英] How To Save Content of GTKTextBuffer to a File

查看:255
本文介绍了如何将GTKTextBuffer的内容保存到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C和GTK在Anjuta上的Ubuntu 12.04上编写程序.它是nbc(乐高NXT编译器)的图形界面.我有一个GTKTextView.现在,我想将textview的内容保存到一个文件中,该文件可以由GTKFileChooser选择.现在,我不知道如何从TextView中获取文本并将其写入文件.我该怎么做?

I'm writing on Ubuntu 12.04 in Anjuta with C and GTK a program. It's a graphical interface for the nbc (Lego NXT Compiler). I have a GTKTextView. Now I want to save the content of the textview to a file, which could be chosen by a GTKFileChooser. Now I don't know how to get the text from the TextView and write it to the file. How do i do this?

推荐答案

首先,使用gtk_text_view_get_buffer()GtkTextView中获取GtkTextBuffer.然后从缓冲区获取开始和结束 GtkTextIters ,以用于获取缓冲区的文本.最后,使用您选择的API将文本写入文件,但是,我建议使用Gio.这是我的旧教程中的摘录:

First, get the GtkTextBuffer from the GtkTextView using gtk_text_view_get_buffer(). Then get the start and end GtkTextIters from the buffer to use to get the text of the buffer. Finally, write that text to the file using the API of your choice, however, I would recommend Gio. Here's a snippet from my old tutorial:

gtk_widget_set_sensitive (text_view, FALSE);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (editor->text_view));
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);
text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);       
gtk_text_buffer_set_modified (buffer, FALSE);
gtk_widget_set_sensitive (editor->text_view, TRUE);

/* set the contents of the file to the text from the buffer */
if (filename != NULL)        
    result = g_file_set_contents (filename, text, -1, &err);
else
    result = g_file_set_contents (editor->filename, text, -1, &err);

if (result == FALSE)
{
    /* error saving file, show message to user */
    error_message (err->message);
    g_error_free (err);
}        

g_free (text); 

查看以下API文档:

  1. http://developer.gnome.org/gtk3/stable/GtkTextBuffer.html
  2. http://developer.gnome.org/glib/stable/glib-File- Utilities.html
  1. http://developer.gnome.org/gtk3/stable/GtkTextBuffer.html
  2. http://developer.gnome.org/glib/stable/glib-File-Utilities.html

这篇关于如何将GTKTextBuffer的内容保存到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆