GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 3:31 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: FileChooserButton write file name to char
PostPosted: Thu Feb 09, 2012 10:52 pm 
Offline
Familiar Face

Joined: Thu Feb 09, 2012 9:20 pm
Posts: 5
Hellow!

I want to get file name user select by FileChooserButton. I tried to do it this way:

Code:
//declaration of global widget and array for file name
GtkWidget *fc_button_o;
char file_name_i;

//callback function for file name writing in array
void set_file_name ()
{
printf ("Setting input file name\n");
file_name_i = (char) gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (fc_button_o));
printf ("Input file name is: %s\n", file_name_i);
}


int main(int argc, char *argv[]) // of course only important code part here
{
fc_button_o = gtk_file_chooser_button_new ("Browse", GTK_FILE_CHOOSER_ACTION_OPEN);
g_signal_connect (G_OBJECT (fc_button_o), "file-set", G_CALLBACK (set_file_name), NULL);
}


Then I try to compile this in Code::Blocks IDE and push the file chooser button which gives me:

Code:
Setting input file name
Segmentation fault


which means that something wrong with string containing gtk_file_chooser_get_filename function. Please help me to find out what is wrong.


Top
 Profile  
 
 Post subject: Re: FileChooserButton write file name to char
PostPosted: Fri Feb 10, 2012 5:33 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Ah this is an easy one. You're trying to store a filename in a single digit character (char). The segfault you are getting then isn't the function but casting it into something that really isn't going to fit (either that or its that your printf expects a null terminated character string but doesn't get this, this might be more likely). The data type you need is a null terminated string (char*) The star makes it a pointer to chars, so you can index more than one. Even better is to call it gchar*; using glibs data types and to use the g_sprintf etc functions for string manipulation as they are more platform independent.


Top
 Profile  
 
 Post subject: Re: FileChooserButton write file name to char
PostPosted: Fri Feb 10, 2012 11:51 am 
Offline
Familiar Face

Joined: Thu Feb 09, 2012 9:20 pm
Posts: 5
Choosing *char helped. Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group