list files in directory
the_y_man

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

Chris Katko

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.

23yrold3yrold

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 ;)

Billybob

yes, allegro has functions for this. Also, you can use the system to your advantage:

Windows:

system("dir >> ls.log");

Linux:

system("ls >> ls.log");

and then crack open the resulting file and read in according to the OS, since dir will produce results differently then ls.

gillius

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 ;).

the_y_man
William Heatley said:

Windows:
system("dir >> ls.log");

Linux:
system("ls >> ls.log");

how would i prevent a dos box from opening?

gillius

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.

the_y_man

bad style, eh? if so, i will avoid it. Thanks for the heads up.

kronoman

Try this example:

1#include <allegro.h>
2#include <stdio.h>
3 
4void my_callback(const char *filename, int attrib, int param)
5{
6 printf("File: %s\n", filename);
7}
8 
9int main()
10{
11allegro_init();
12 
13for_each_file("*.txt", FA_ARCH | FA_HIDDEN | FA_SYSTEM , my_callback , 0);
14 
15return 0;
16}
17END_OF_MAIN();

Evert

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.

Billybob

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 ;D

heh

CGamesPlay

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

kronoman

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);

Thread #309233. Printed from Allegro.cc