gpt4 book ai didi

c - 如何从 gtktextview/gtksourceview 解析 C 中的文本

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:01 24 4
gpt4 key购买 nike

我正在使用 gtk+-2.0 和 gtksourceview-2.0 用 C 编写一个文本编辑器。我正在尝试对从缓冲区中提取的数据运行一些测试。目前,我正在移动一对迭代器,然后获取由这些迭代器分隔的文本 block 。然后使用 strstr、strchr、...检查此字符串。但是,这个移动迭代器、然后读取数据、然后执行测试的过程非常繁琐。

我还提取了整个文档(使用前面描述的过程)到 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 *’

我正在寻找一种检查方法,例如,通过简单地从循环索引中减去一个来检查前一行,就像每一行都是数组的一个元素一样。我怎样才能把数据放到这个表格中?请耐心等待,我正在学习。谢谢。

最佳答案

这是一个如何从 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;
}

有关更多详细信息,请阅读 documentation的 GtkTextIter。

关于c - 如何从 gtktextview/gtksourceview 解析 C 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com