AhmetHani
AhmetHani

Reputation: 33

Gtk+ File handling with C

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 );
}

Upvotes: 3

Views: 1894

Answers (2)

Wiky
Wiky

Reputation: 152

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

Upvotes: 9

liberforce
liberforce

Reputation: 11454

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.

Upvotes: 0

Related Questions