Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] al_read_directory()

This thread is locked; no one can reply to it. rss feed Print
[A5] al_read_directory()
William Labbett
Member #4,486
March 2004
avatar

Hi, got a crash with al_read_directory().

It occurs with this line :

file = al_read_directory( working_dir );

Tried everything I can think of to solve it but haven't worked it out.

Anyone see what's wrong ?

Thanks

#SelectExpand
1al_open_directory( working_dir ); 2 3 count = 0; 4 5 for( fn = 0; fn < num_files; ++fn) 6 { 7 printf(" fn is %d.\n", fn); 8 9 if(working_dir == NULL) 10 { 11 printf(" working_dir NULL.\n"); 12 return QUIT_PROGRAM; 13 } 14 15 printf(" checked working_dir isn't NULL.\n"); 16 17 file = al_read_directory( working_dir ); 18 19 printf(" read directory.\n"); 20 21 if(file == NULL) 22 { 23 printf(" file is NULL.\n"); 24 return QUIT_PROGRAM; 25 26 }

EDIT : theanks to anyone who tried to solve it. The answer wasn't possible to see from the source I gave.

The problem was with this code :

#SelectExpand
1 2frd_filenames = malloc( sizeof(char) * num_frd_files ); 3 4 for(fn = 0; fn < num_frd_files; ++fn) 5 { 6 frd_filenames[fn] = malloc( sizeof(char) * (longest_name + 1)); 7 } 8 9 10 printf(" allocated memory.\n"); 11 12 13 //al_destroy_fs_entry(working_dir); 14 15 //working_dir = al_create_fs_entry(al_get_current_directory()); 16 17 al_open_directory( working_dir ); 18 19 count = 0; 20 21 for( fn = 0; fn < num_files; ++fn) 22 { 23 printf(" fn is %d.\n", fn); 24 25 if(working_dir == NULL) 26 { 27 printf(" working_dir NULL.\n"); 28 return QUIT_PROGRAM; 29 } 30 31 printf(" checked working_dir isn't NULL.\n"); 32 33 file = al_read_directory( working_dir ); 34 35 printf(" read directory.\n"); 36 37 if(file == NULL) 38 { 39 printf(" file is NULL.\n"); 40 return QUIT_PROGRAM; 41 42 } 43 44 /* const char * */ frd_filename = al_get_fs_entry_name( file ); 45 46 if( frd_filename == NULL) 47 { 48 printf(" frd_filename NULL.\n"); 49 return QUIT_PROGRAM; 50 } 51 52 path = al_create_path( frd_filename ); 53 54 extension = al_get_path_extension( path ); 55 56 printf(" extension = %s", extension); 57 58 printf(" comparing string.\n"); 59 60 if( strcmp( extension, ".frd") == 0) 61 { 62 63 strcpy(frd_filenames[count], al_get_path_filename( path )); 64 printf("%s\n", frd_filenames[count]); 65 ++count; 66 printf(" count is %d.\n", count); 67 } 68 69 70 71 al_destroy_path(path); 72 //al_destroy_fs_entry(file); 73 74 }

This line

frd_filenames = malloc( sizeof(char) * num_frd_files );

should have been

frd_filenames = malloc( sizeof(char *) * num_frd_files );

Not sure why it was causing the crash though.

Arthur Kalliokoski
Second in Command
February 2005
avatar

frd_filenames = malloc( sizeof(char) * num_frd_files );

only allocates "num_frd_files" bytes. You can't put a filename (with terminator) into one char, and you can't fit a pointer into it (4 bytes for 32 bit OS, 8 bytes for 64 bit).

They all watch too much MSNBC... they get ideas.

Go to: