I'm really clueless on this. But how would I, in C or C++ get a list of all files (and maybe store the file names in an array of strings)? I'm not asking you to write code for me, I'm mostly asking for some hints. Thanks.:o
I believe allegro provides specific functions for this very purpose.
[edit]
int for_each_file_ex(const char *name, int in_attrib, int out_attrib, int (*callback)(const char *filename, int attrib, void *param), void *param);
Under file and compression routines. Sidenote: I wonder why Allegro.CC doesn't have for_each_file_ex() in it's help database for (code)(/code) blocks.
Yup. And storing them in an std::vector of std::string's would be just too easy
If you're looking for a non-Allegro solution, poach Allegro's source
For an interesting lemony twist, I believe Boost provides a library that represents a file system in an STL-like container/tree. Allegro's function is great too but I've used neither so I cannot comment more .
Please don't take that suggestion (ls or dir) seriously. That's a very bad way of getting a listing of files. Very bad style and approach and technique. Use one of the other ways we've suggested.
bad style, eh? if so, i will avoid it. Thanks for the heads up.
Try this example:
1 | #include <allegro.h> |
2 | #include <stdio.h> |
3 | |
4 | void my_callback(const char *filename, int attrib, int param) |
5 | { |
6 | printf("File: %s\n", filename); |
7 | } |
8 | |
9 | int main() |
10 | { |
11 | allegro_init(); |
12 | |
13 | for_each_file("*.txt", FA_ARCH | FA_HIDDEN | FA_SYSTEM , my_callback , 0); |
14 | |
15 | return 0; |
16 | } |
17 | END_OF_MAIN(); |
If you want to be independent of Allegro, you can look into the POSIX functions opendir() readdir() and closedir(). They are portable to all UNIX platforms and variants of GCC (including DJGPP and MinGW). They won;t work with MSVC though.
Mine is what you call the hacky/dodgy way of doing things Just a suggestion, so shrugs whatever. At least it was an original idea in this topic and quite clever if you ask me
heh
for_each_file_ex isn't in the .cc help system because it's a WIP function. for_each_file might not suit your needs (for instance if you only want the files that are directories), but it's the only one in 4.0.1
You can also use:
With this, you can list files, and doing recursive function, for example, list all files in a drive.
If you only want the directories, check against FA_DIRECT flag.
i.e
if (f.ff_attrib & FA_DIREC) do_something(f);