Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Creating an array of files

This thread is locked; no one can reply to it. rss feed Print
Creating an array of files
nshade
Member #4,372
February 2004

I'm trying to create an array of files using al_for_each_fs_entry(), however when I try and index them, it's not longer getting all the files..

Example Code

#SelectExpand
1#include <allegro5/allegro.h> 2#include <stdio.h> 3 4 5char fnamelist[1000][255]; 6int fileindex = 0; 7int maxfileindex = 0; 8 9void save_file(ALLEGRO_FS_ENTRY *entry) 10{ 11 char *name = al_get_fs_entry_name(entry); 12 //strcpy(fnamelist[fileindex], name); 13 printf("%s\n", name); 14 //fileindex++; 15} 16 17 18int create_file_list(char *path, char *fpath, char *fname) 19{ 20 int i; 21 fileindex = 0; 22 for (i = 0; i < 999; i++) 23 { 24 strcpy(fnamelist[i], "\0"); 25 } 26 27 28 ALLEGRO_FS_ENTRY *entry = al_create_fs_entry(path); 29 al_for_each_fs_entry(entry, save_file, al_get_fs_entry_name(entry)); 30} 31 32 33int main(int argc, char **argv) 34{ 35 36 char *path = "data"; 37 38 if (!al_init()) { 39 printf("Could not init Allegro.\n"); 40 } 41 42 43 create_file_list("data", NULL, NULL); 44 45 return 0; 46}

When this is ran, all files in the "data" folder will print out, however, the moment I uncomment "//fileindex++;" (line 14) it will only print two files and then stop... What am I doing wrong that is causing this?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

That shouldn't even compile.

http://liballeg.org/a5docs/trunk/fshook.html#al_for_each_fs_entry

int al_for_each_fs_entry(ALLEGRO_FS_ENTRY *dir,
                         int (*callback)(ALLEGRO_FS_ENTRY *entry, void *extra),
                         void *extra)

This means your 'save_file' function has the wrong function signature.

Here's working example code :

#SelectExpand
1/// Global 2#include <vector> 3#include <string> 4 5std::vector<std::string> entries; 6 7int AddEntry(ALLEGRO_FS_ENTRY* entry , void* extra) { 8 (void)extra; 9 entries.push_back(std::string(al_get_fs_entry_name(entry))); 10 printf("Found '%s' in home directory.\n" , entries.back().c_str()); 11 12 return ALLEGRO_FOR_EACH_FS_ENTRY_OK; 13} 14 15 16/// Main 17 18 /// Test code for searching a directory 19 20 ALLEGRO_PATH* homepath = al_get_standard_path(ALLEGRO_EXENAME_PATH); 21 al_set_path_filename(homepath , 0); 22 23 ALLEGRO_FS_ENTRY* home = al_create_fs_entry(al_path_cstr(homepath , '/')); 24 printf("Home (%s) %s\n" , al_get_fs_entry_name(home) , al_fs_entry_exists(home)?"exists":"not found"); 25 al_destroy_path(homepath); 26 al_for_each_fs_entry(home , AddEntry , 0); 27 al_destroy_fs_entry(home); 28 29 printf("Home directory enumeration complete.\n");

nshade
Member #4,372
February 2004

Thanks for the help...

The old code did compile, I promise! I blame either Allegro's exception handler or Visual Studio for allowing my awful code to compile :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

That is part of why I dislike Visual Studio. I noticed that it allows quite a few problems to slip by without so much as a warning.

---
“I love you too.” - last words of Wanda Roy

Go to: