no direct fscanf replacement I know of. I use g_file_get_contents and then g_strsplit.
At the moment your %s will delimit off whitespace which will not make much sense when you are using latex tags. You'd be better off storing entire lines or for the case of latex/bibtex delimit off two consecutive EndOfLine markers (and then potentially further according to latex curly bracketing or key=value entries for bibtex). g_strsplit gives a good deal of flexibility in how you might want to split up the text.
I don't get any segfaults with things changed over to this system. Here's a little example:
Code:
#include <glib.h>
int main(int argc, char** argv)
{
gint j;
gchar** alst;
gchar* cts=NULL;
GError* Err=NULL;
GArray* a=g_array_new(FALSE, FALSE, sizeof(gchar*));
if (g_file_get_contents("/home/paul/test.tex", &cts, NULL, &Err))
{
alst=g_strsplit_set(cts, "\n", 0);
g_free(cts);
j=0;
g_print("blah");
while (alst[j])
{
g_array_append_val(a, alst[j]);
g_print("%s", g_array_index(a, gchar*, j++));
}
g_strfreev(alst);
}
else
{
g_print("Loading failed, error: %s.", Err->message);
g_error_free(Err);
}
g_print("There are now %d items in the array\n", a->len);
g_array_free(a, FALSE);
return 0;
}
Of course there's no real need for the garray in this example, it's just for demonstration. Though you may perform further manipulations that may make it useful over the char** case.
You might note the lack of buffers in this case through the advantage of using built in dynamic allocation of memory. Thus there are no overflows to worry about.
Not sure why you are getting segfaults yet. Will look into it.