Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » list files in directory

Credits go to 23yrold3yrold, Billybob, Chris Katko, and gillius for helping out!
This thread is locked; no one can reply to it. rss feed Print
list files in directory
the_y_man
Member #1,770
December 2001
avatar

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
Member #1,881
January 2002
avatar

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.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

23yrold3yrold
Member #1,134
March 2001
avatar

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

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Billybob
Member #3,136
January 2003

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
Member #119
April 2000

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

Gillius
Gillius's Programming -- https://gillius.org/

the_y_man
Member #1,770
December 2001
avatar

William Heatley said:

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

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

how would i prevent a dos box from opening?

gillius
Member #119
April 2000

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.

Gillius
Gillius's Programming -- https://gillius.org/

the_y_man
Member #1,770
December 2001
avatar

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

kronoman
Member #2,911
November 2002
avatar

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
Member #794
November 2000
avatar

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
Member #3,136
January 2003

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
Member #2,559
July 2002
avatar

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

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

kronoman
Member #2,911
November 2002
avatar

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

Go to: