GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Sep 19, 2014 3:51 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: problems getting file list
PostPosted: Tue Apr 15, 2008 10:52 am 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
I'm following the example at "Foundations of GTK+" where it explains how to get a folder's content.
I'm using this code to store each filename inside a vector:

Code:
        GDir* dir = g_dir_open( "fonts/", 0, NULL );
   const gchar* file;
        std::vector<char*> files;

   if( !g_file_test( "fonts/", G_FILE_TEST_IS_DIR ) )
      g_error( "Error: There is no \"fonts\" directory!" );

   while( ( file = g_dir_read_name( dir ) ) )
   {
      std::cout << "File:" << file << std::endl;
           files.push_back( (char*)file );
   }
        g_dir_close( dir );


while cout prints ok each filename, doesn't stores correctly into files vector. It seems like I'm storing the address instead the value of file because if I remove g_dir_close(dir) I get almost valid values.

I say almost 'cause what I'm just getting the last filename found repeated number_of_files_on_folder times.

What I'm doing wrong???[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 2:22 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Use the following code instead. This way, your string pointer is not overwritten each time.

Code:
files.push_back( g_strdup (file) );

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 2:47 pm 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
that worked!
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: Google [Bot] and 1 guest


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