我有一个功能:
//In main.c
char output_entry () {
extern FILE* yyin;
extern int yyparse (void);
yyin=fmemopen(buffer,strlen(buffer),"r");
return yyparse();
}
从调用时可以正常工作
//Open file
gchar *filename;
void open_file(GtkWidget *widget, gpointer data)
{
GError* error=NULL;
GtkWidget *dialog;
GtkFileFilter *filter;
dialog = gtk_file_chooser_dialog_new("Open File", NULL,
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
NULL);
filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, "All files (*.*)");
gtk_file_filter_add_pattern(filter, "*");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
filter = gtk_file_filter_new();
gtk_file_filter_set_name(filter, "Bibtex file (*.bib)");
gtk_file_filter_add_pattern(filter, "*.bib");
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
{
gtk_list_store_clear (store);
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
g_file_get_contents(filename, &buffer, &length , &error);
g_assert(!error);
buf_mod=FALSE;
char* markup=g_markup_printf_escaped ("<span style=\"italic\">%s</span>", filename);
gtk_label_set_markup(GTK_LABEL(flabel), markup);
gtk_widget_destroy(dialog);
output_entry();
}
else{
gtk_widget_destroy(dialog);
// g_free(buffer);
}
}
这是打开文件的标准方法。并且buffer
是
//in main.h
extern gchar *buffer;
并在 main.c 中初始化(如此处建议)
这部分代码工作正常。我也尝试将 output_entry 与其他一些来源一起使用:
void gs_open(GtkWidget *window, gpointer data) {
GScanner *gs_scanner;
GHashTable *gs_table;
GError* error=NULL;
GtkTextIter start, end;
GtkListStore *gs_store;
GtkTreeIter siter;
GtkWidget *gs_tree;
gboolean valid;
GString *ustring = g_string_new ("");
GString *str=g_string_new(NULL);
GtkTextBuffer *gs_buf=gtk_text_view_get_buffer(GTK_TEXT_VIEW(gs_txt));
gtk_text_buffer_get_start_iter (gs_buf, &start);
gtk_text_buffer_get_end_iter (gs_buf, &end);
gchar *gs_text = gtk_text_buffer_get_text (gs_buf, &start, &end, FALSE);
strcat(buffer, gs_text);
gtk_list_store_clear(store);
output_entry ();
buf_mod=TRUE;
gtk_widget_destroy(gtk_widget_get_toplevel (window));
}
这给出了段错误。使用 gdb 运行显示:
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff16fc700 (LWP 6178)]
Program received signal SIGSEGV, Segmentation fault.
0x0000003b94097261 in __strcat_sse2_unaligned () from /lib64/libc.so.6
请帮助(我是 C 的新手)。谷歌在这方面帮助不大strcat_sse2_unaligned
编辑 2
up
#1 0x0000000000407195 in gs_open (window=0x863a80, data=<optimized out>)
at src/search.c:103
103 strcat(buffer, gs_text);