我正在用C语言编写一个文本编辑器,使用gtk + -2.0& gtksourceview-2.0。我正在尝试对我从缓冲区中提取的数据进行一些测试。目前我正在移动一对iters然后获取由这些iters划分的文本块。然后使用strstr,strchr,...检查这个字符串。然而,这个移动iters,然后读取数据,然后执行测试的过程非常麻烦。
我还将整个文档(使用前面描述的过程)提取到gchar *
。处理这些数据会更容易吗?我试图在这些数据上使用fgets和getline,但编译器抱怨:
warning: passing argument 3 of ‘fgets’ from incompatible pointer type /usr/include/stdio.h:624:14: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘gchar *’
或
warning: passing argument 1 of ‘getline’ from incompatible pointer type /usr/include/stdio.h:671:20: note: expected ‘char ** __restrict__’ but argument is of type ‘gchar *’
我试过了:
int bytes_read;
int nbytes = 100;
char *my_string;
GtkTextIter start;
GtkTextIter end;
wholeDoc = gtk_text_buffer_get_text(tbuffer,&start,&end,TRUE);
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline (&my_string, &nbytes, wholeDoc);
但出现以下错误:
warning: pointer targets in passing argument 2 of ‘getline’ differ in signedness /usr/include/stdio.h:671:20: note: expected ‘size_t * __restrict__’ but argument is of type ‘int *’
我正在寻找一种方法来检查,例如,前一行只需从循环索引中减去一行,就像每行是数组的元素一样。如何将数据输入此表单?请耐心等待,我正在学习。感谢。
答案 0 :(得分:1)
这是一个如何从GtkTexBuffer获取行的简单示例。
GtkTextIter start_iter, next_iter;
gchar *text;
gtk_text_buffer_get_iter_at_offset (source_buffer, &start_iter, 0);
next_iter = start_iter;
while (gtk_text_iter_forward_line (&next_iter))
{
text = gtk_text_iter_get_text (&start_iter, &next_iter);
// line processing
g_free (text);
start_iter = next_iter;
}
有关详细信息,请阅读GtkTextIter的documentation。
答案 1 :(得分:0)
您可以将字符串拆分为字符串数组,每行一个。使用g_strsplit()
。但是,如果要将数据关联回缓冲区中的位置,最好使用缓冲区迭代器。