Write the text from a gtk entry to a file?
Hello everyone, I found the code below for a gtk entry dialog, and extended the code to write the output into a file. What is the usual way to do this? I dried do open and close the file in the main function. But i did'n know how do give it as argument to the button_clicked function. In this working one i have the open and close function in the button_clicked function. Is this a good solution, or how can i declare the file in the main. Open it in the main and just give the file as argument to the sub function? Kind regards Max #include <iostream> #include <gtk/gtk.h> #include <fstream> using namespace std; void button_clicked( GtkWidget *widget, gpointer data ) { ofstream myfile; myfile.open("test.txt",fstream::out | fstream::in | fstream::app); g_print("%s\n",gtk_entry_get_text(GTK_ENTRY(data))); myfile << gtk_entry_get_text(GTK_ENTRY(data)) << '\n'; myfile.close(); } int main( int argc, char* argv[] ) { // ofstream myfile; // myfile.open("test.txt",fstream::out | fstream::in | fstream::app); // myfile << zeile; gtk_init(&argc,&argv); // Initalisation GtkWidget *window, *entry1, *button1, *grid; // declare needet variables window=gtk_window_new(GTK_WINDOW_TOPLEVEL); // Create window g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit), NULL); gtk_container_set_border_width (GTK_CONTAINER (window), 10); grid = gtk_grid_new (); // Create Grid //Pack the container in the window gtk_container_add (GTK_CONTAINER (window), grid); // here starts the prog entry1=gtk_entry_new(); button1=gtk_button_new_with_mnemonic("Some text1"); gtk_grid_attach (GTK_GRID (grid), button1, 1, 0, 1, 1); gtk_grid_attach (GTK_GRID (grid), entry1, 0, 0, 1, 1); g_signal_connect(button1,"clicked",G_CALLBACK(button_clicked), entry1); g_signal_connect(entry1,"activate",G_CALLBACK(button_clicked), entry1); gtk_widget_show_all(window); // show all widgets gtk_main(); //start main loop return 0; }