Archived

This topic is now archived and is closed to further replies.

Directory listings

This topic is 6918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi guys and gals, Ok, here is a little teaser. How does one find the contents of a directory in C/C++ without using any windows specific functions? Ie: If I now the directory that I want to search, I want to get the first file in it and then step through each file getting each filename. --== Rapier ==-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Save the whales, feed the hungry, free the mallocs!

Share on other sites
You can use findfirst, findnext and findclose to get the files in a directory.

Problem is, though, that not all compilers use the same names and/or same include files for those functions. VC++ 6, for example, puts these functions in io.h and prepends an underscore. BCC 5.5 places them in dir.h and doesn''t use an underscore. On other platforms, it''s still different...

Erik

Share on other sites
Er, it says in the Visual C++ docs that _findfirst, _findlast, etc etc are Windows specific functions. Which was not what was asked for Now, I''m no C++ standard expert, but it''s possible there are no standardised directory-level functions, since they would be platform-dependent. I get a feeling that the closest you will get is the POSIX stuff (opendir, readdir, closedir and rewinddir) except that MSVC doesn''t seem to support them...

Share on other sites
*Nods to Kylotan* Yeah, thats what I thought about findfirst etc.

The thing is, I need to write a this thing to run on what is -probably- going to be a unix server, and I need to parse a directory for n amount of files, the names of which I wont know.

Anybody got any ideas?

--== Rapier ==--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Save the whales, feed the hungry, free the mallocs!

Share on other sites
You may have to just do this:
FilenameList StoreDirectoryInList(String theDirectory){    FilenameList theList;#ifdef WIN32    // Do it the Win32 way here#else    // Do it the Unix way here#endif // WIN32        return theList;}

If you put it into a wrapper like this, the rest of your code never needs to care whether it is running on Unix or Win32.

Edited by - Kylotan on 5/5/00 2:59:07 PM

Share on other sites
A fine idea, but what -IS- the Unix way?

Share on other sites
I believe POSIX specifies opendir(), readdir(), closedir(), etc. for scanning directory entries in the file system.

...

Share on other sites
quote:
Original post by SiCrane

I believe POSIX specifies opendir(), readdir(), closedir(), etc. for scanning directory entries in the file system.

Yep. You''re right. Those functions are part of POSIX.1, XPG4, XPG4-UNIX standards. They are supported by BSD 4.3, too. Using those functions you''ll get quite portable program.

BTW, referring to the original posting, there''s a nice and useful function that you could use to fetch the content of a specific directory (eventually applying a filter to it): scandir(). In fact, it''s a "macro-function" that uses the primitives you mentioned. It''s prototype follows:

int scandir(const char *dir, struct dirent ***namelist, int (*select)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **));

More details if needed...

Bye,

Karmalaa /

Share on other sites
Thats exactly what I am looking for. Thanks a lot. You have all been, as always, a great help.

Thanks.

--== Rapier ==--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Save the whales, feed the hungry, free the mallocs!

• 10
• 13
• 52
• 11
• 15