GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

opening a file and writing data in the file by gtk+

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Turtel
Familiar Face


Joined: 01 Mar 2010
Posts: 34
Location: India

PostPosted: Fri Mar 19, 2010 4:28 am    Post subject: opening a file and writing data in the file by gtk+ Reply with quote

Could anyone give a small example to show how to open a file and write data on to it through press of a button.............??
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Sat Mar 20, 2010 1:03 am    Post subject: Reply with quote

Hello.

This crude sample should get you up-and-going, but be aware that my code doesn't check for any errors that may occur while saving, loading ... Be careful when using this application, since it'll overwrite any file you designate as a save location.

And the code:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <gtk/gtk.h>

typedef struct _Data
{
    GtkWidget     *main_window;
    GtkTextBuffer *buffer;
}
Data;

static void
cb_open( GtkButton *button,
         Data      *data )
{
    GtkWidget *dialog;

    dialog = gtk_file_chooser_dialog_new( "Open file",
                                          GTK_WINDOW( data->main_window ),
                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                          GTK_STOCK_OPEN, GTK_RESPONSE_OK,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          NULL );
    if( GTK_RESPONSE_OK == gtk_dialog_run( GTK_DIALOG( dialog ) ) )
    {
        gchar *filename;
        gchar *string;
       
        filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
        g_file_get_contents( filename, &string, NULL, NULL );
        g_object_set( G_OBJECT( data->buffer ), "text", string, NULL );
        g_free( filename );
        g_free( string );
    }
    gtk_widget_destroy( dialog );
}

static void
cb_save( GtkButton *button,
         Data      *data )
{
    GtkWidget *dialog;

    dialog = gtk_file_chooser_dialog_new( "Save file",
                                          GTK_WINDOW( data->main_window ),
                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                          GTK_STOCK_SAVE, GTK_RESPONSE_OK,
                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                          NULL );
    if( GTK_RESPONSE_OK == gtk_dialog_run( GTK_DIALOG( dialog ) ) )
    {
        gchar *filename;
        gchar *string;
       
        filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
        g_object_get( G_OBJECT( data->buffer ), "text", &string, NULL );
        g_file_set_contents( filename, string, -1, NULL );
        g_free( filename );
        g_free( string );
    }
    gtk_widget_destroy( dialog );
}

int
main( int    argc,
      char **argv )
{
    GtkWidget *window,
              *table,
              *swindow,
              *text,
              *button;
    Data      *data;

    gtk_init( &argc, &argv );

    data = g_slice_new0( Data );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    table = gtk_table_new( 2, 2, FALSE );
    gtk_container_add( GTK_CONTAINER( window ), table );

    swindow = gtk_scrolled_window_new( NULL, NULL );
    gtk_table_attach( GTK_TABLE( table ), swindow, 0, 2, 0, 1,
                      GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0 );

    text = gtk_text_view_new();
    gtk_container_add( GTK_CONTAINER( swindow ), text );
    data->buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( text ) );

    button = gtk_button_new_from_stock( GTK_STOCK_OPEN );
    g_signal_connect( G_OBJECT( button ), "clicked",
                      G_CALLBACK( cb_open ), data );
    gtk_table_attach( GTK_TABLE( table ), button, 0, 1, 1, 2,
                      GTK_FILL, GTK_FILL, 0, 0 );

    button = gtk_button_new_from_stock( GTK_STOCK_SAVE_AS );
    g_signal_connect( G_OBJECT( button ), "clicked",
                      G_CALLBACK( cb_save ), data );
    gtk_table_attach( GTK_TABLE( table ), button, 1, 2, 1, 2,
                      GTK_FILL, GTK_FILL, 0, 0 );

    gtk_widget_show_all( window );

    gtk_main();

    g_slice_free( Data, data );

    return( 0 );
}

Tadej
Back to top
Turtel
Familiar Face


Joined: 01 Mar 2010
Posts: 34
Location: India

PostPosted: Sat Mar 20, 2010 7:07 am    Post subject: Reply with quote

thaks very much tadeboro ........ you shall be ever remembered for your cost free services to the open community.............hats off to you.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP