3

Does anyone know file handling using GTK. I need to open a file in "w+" mode and write to it. I dont know if file handling exists in GTK. Below is a sample 'C' program that I need in GTK format.

#include <stdio.h>

int main(int argc, char *argv[]) {

 int a = 5;
 float b = 3.14;
 FILE *fp;

 fp = fopen( "test.txt", "w+" );
 fprintf( fp, "  %d   %f", a, b ); 
 fwrite(----);
 fclose( fp );
}
devnull
  • 118,548
  • 33
  • 236
  • 227
AhmetHani
  • 33
  • 3
  • gtk is a graphics toolkit. you are not doing ANY graphics operations in the code. That's just plain-jain C code. – Marc B Oct 09 '13 at 21:36
  • GTK+ programs are a superset of plain C programs, so if the above code works I don't see any reason to change it. Gio could be useful only if you need I/O integration with the main GTK+ loop. – ntd Oct 15 '13 at 15:47

2 Answers2

9

I'm sure you mean GIO

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    gtk_init(&argc,&argv);

    GFile *file=g_file_new_for_path("test.txt");
    GFileOutputStream *output=g_file_replace(
                file,NULL,FALSE,
                G_FILE_CREATE_NONE,
                NULL,NULL);
    gint a=5;
    gfloat  b=3.14;
    gchar *buf=g_strdup_printf(" %d %f",a,b);
    g_output_stream_write(G_OUTPUT_STREAM(output),
                buf,strlen(buf),NULL,NULL);
    /* ----- */
    g_output_stream_close(G_OUTPUT_STREAM(output),NULL,NULL);
    g_free(buf);
    g_object_unref(output);
    g_object_unref(file);
    return 0;
}

I won't explain any function,see GIO Reference Manual(do you know Devhelp?) for more detials

Wiky
  • 152
  • 7
0

As said above, GTK is for user interfaces, and your code sample has no graphical user interface. However, you may give a look to GLib, the underlying library that powers GTK. There are file utilities functions, good for portability across systems, and portable I/O channels.

liberforce
  • 11,189
  • 37
  • 48