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 

problems getting file list

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


Joined: 10 Apr 2008
Posts: 19

PostPosted: Tue Apr 15, 2008 10:52 am    Post subject: problems getting file list Reply with quote

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: (C++)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

        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]
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 372
Location: State College, Pennsylvania

PostPosted: Tue Apr 15, 2008 2:22 pm    Post subject: Reply with quote

Use the following code instead. This way, your string pointer is not overwritten each time.

Code: (Plaintext)
1
files.push_back( g_strdup (file) );
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Tue Apr 15, 2008 2:47 pm    Post subject: Reply with quote

that worked!
Thank 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