Skip to main content
GameDev.net gamedev.net
🔒 Locked

C/C++ Standard library "find file" function

Started by XTAL256 Jun 28, 2008 at 7:10 PM 16 replies 2.4k views
Original Post
XTAL256
XTAL256
I need a function that finds all files/folders within a given directory. I want to use a standard function so that i can port my game, so i don't want to use Windows' FindFirstFile and FindNextFile functions, but i can't find any such function in the C or C++ standard library. Can anyone help me? thanks
[Window Detective] - Windows UI spy utility for programmers
nullsquared
nullsquared
Quote:
Original post by XTAL256
I need a function that finds all files/folders within a given directory. I want to use a standard function so that i can port my game, so i don't want to use Windows' FindFirstFile and FindNextFile functions, but i can't find any such function in the C or C++ standard library. Can anyone help me?
thanks


Pretty sure the standard library doesn't contain such functionality, but you might find boost.filesystem useful for this.
Zakwayda
Zakwayda
Quote:
...but you might find boost.filesystem useful for this.
Yup, boost::filesystem is probably your best option here.
XTAL256
XTAL256
Well i did download Boost a while ago but i can't be bothered installing it/setting it up, and i don't want to include it in my game just to use one function. If anything, i would rather use a small cross-platform library. Unless there is a way to do it using std lib, like using I/O to search a directory (?).
[Window Detective] - Windows UI spy utility for programmers
fpsgamer
fpsgamer
Quote:
Original post by Telastyn
The Posix standard has some C functions guaranteed to be on pretty much any modern OS. They're pretty ugly though.


He is referring to dirent.h, which is a POSIX API and Windows does not implement natively.

Though there are wrappers that make this API available on Windows. A couple come up if you google it.
Telastyn
Telastyn
Quote:
Original post by fpsgamer
Quote:
Original post by Telastyn
The Posix standard has some C functions guaranteed to be on pretty much any modern OS. They're pretty ugly though.


He is referring to dirent.h, which is a POSIX API and Windows does not implement natively.

Though there are wrappers that make this API available on Windows. A couple come up if you google it.


Eh thank you. It's been ages since I've used that stuff; had forgotten the name.
Zakwayda
Zakwayda
Quote:
Well i did download Boost a while ago but i can't be bothered installing it/setting it up, and i don't want to include it in my game just to use one function.
As far as installing Boost goes, on Windows it should be very easy: just use the installers from BoostPro. I believe there are pre-built binaries for Linux as well. I'm not sure about OS X, so you might have to build it there.

As for including it in your game, I'm going to venture a guess that the Filesystem library is not that big and won't add significantly to your executable's size (perhaps someone can confirm or deny this). Also, I wouldn't worry about including it 'just to use one function'. The functionality offered by the Filesystem library is very handy, and I'm willing to bet you'd find other places to use it.

In any case, it sounds like you're willing to incorporate some third-party code into your project, so it seems to me it might as well be Boost.Filesystem (which is tried and tested, and does exactly what you want).
XTAL256
XTAL256
Ok, thanks i'll check it out. But for the moment i'll just write code for each OS and use #ifdef. i.e:
#if defined(_WIN32)  FindFirstFile(...)#elif defined(_MAC)   // MacOS code for finding files#endif

Something like that. And i can replace that when i use whatever library i end up using.

[Edited by - XTAL256 on June 29, 2008 8:30:50 PM]
[Window Detective] - Windows UI spy utility for programmers
fpsgamer
fpsgamer
Quote:
Original post by XTAL256
Ok, thanks i'll check it out. But i think it would just be easier to write code for each OS and use #ifdef. i.e:
*** Source Snippet Removed ***
Something like that. After all, that's what most cross-platform libs do, right?


Neither of the options we presented require you to use OS specific macros. Either use the POSIX API on all platforms, or use the boost::filesystem API on all platforms.
XTAL256
XTAL256
Quote:
Original post by fpsgamer
Neither of the options we presented require you to use OS specific macros.

#ifdef isn't OS specific is it? Although from looking at the pre-processor command list on cppreference.com, it looks like #if defined(...) isn't standard.

@jyk: you make some good points. I might install and use Boost, i have been meaning to set it up for a while now but i didn't know there were installers. thanks
[Window Detective] - Windows UI spy utility for programmers
demonkoryu
demonkoryu
Quote:
Original post by XTAL256
Well i did download Boost a while ago but i can't be bothered installing it/setting it up, and i don't want to include it in my game just to use one function. If anything, i would rather use a small cross-platform library. Unless there is a way to do it using std lib, like using I/O to search a directory (?).


Erm, you can't be bothered...? That will surely get you far...

Consider Boost "the missing pieces" of C++, and use it like the std library. ;)

Most parts of Boost are lightweight, and it's cross-platform.
Cromulent
Cromulent
Quote:
Original post by jyk
Quote:
Well i did download Boost a while ago but i can't be bothered installing it/setting it up, and i don't want to include it in my game just to use one function.
As far as installing Boost goes, on Windows it should be very easy: just use the installers from BoostPro. I believe there are pre-built binaries for Linux as well. I'm not sure about OS X, so you might have to build it there.


Dead easy to install on Mac OS X as well. Just use MacPorts. Has the latest version of Boost available (1.35.0).
XTAL256
XTAL256
Quote:
Original post by Konfusius
Quote:
Original post by XTAL256
Well i did download Boost a while ago but i can't be bothered installing it/setting it up, and i don't want to include it in my game just to use one function. If anything, i would rather use a small cross-platform library. Unless there is a way to do it using std lib, like using I/O to search a directory (?).


Erm, you can't be bothered...? That will surely get you far...

Oh, come on! You're being a bit dramatic aren't you? Don't tell me you've never not bothered to do something? The reason i "couldn't be bothered" is because i never had a reason to use Boost. Not to say that it wouldn't be useful, i just always used std lib or something i already had at hand.
[Window Detective] - Windows UI spy utility for programmers
fpsgamer
fpsgamer
Quote:
Original post by XTAL256
Quote:
Original post by fpsgamer
Neither of the options we presented require you to use OS specific macros.

#ifdef isn't OS specific is it? Although from looking at the pre-processor command list on cppreference.com, it looks like #if defined(...) isn't standard.

@jyk: you make some good points. I might install and use Boost, i have been meaning to set it up for a while now but i didn't know there were installers. thanks


Rather things like _WIN32, __WIN32__, __APPLE__, __linux etc. are OS specific macros. If you're using standard APIs there is zero need to use them.

For example, why would you do this:

#if defined( _WIN32 ) || defined( __WIN32__ )     #include <dirent.h>#elif defined( __linux ) || defined( __linux )     #include <dirent.h>#elif defined( __APPLE__ )     #include <dirent.h>#else     # error EVERYBODY PANIC!#endif


The redundancy in the above snippet should be obvious.

oler1s
oler1s
Quote:
The reason i "couldn't be bothered" is because i never had a reason to use Boost. Not to say that it wouldn't be useful, i just always used std lib or something i already had at hand.
Significant functionality is in Boost, and you’ve come across one instance where the standard library is not sufficient. Use Boost, look for some wrapper or library, or write your own code to deal with platform specific APIs. We think Boost is by far the easiest. Does this answer your question? If not, can you restate your question so that we understand why our answer so far has been inadequate, and what more you want to know.
XTAL256
XTAL256
Don't think that i'm not grateful for your advice, i asked a question and you guys helped me, and i thank you for that. But at the end of the day it is my choice and i just don't think that i really need Boost right now. And you guys have posed to choices; Boost or POSIX, so i might end up using POSIX instead. But at least i know i have choices, and that there are ways for me do solve my problem.

EDIT: i have decided to use dirent.h since i found Free Windows implementation of dirent.h on the wikipedia page which has a .lib file just of dirent, so i don't even have to download all of POSIX. Don't worry, i'm sure i'll get around to using Boost some time :)

[Edited by - XTAL256 on June 30, 2008 6:34:06 AM]
[Window Detective] - Windows UI spy utility for programmers
demonkoryu
demonkoryu
A bit late, but...
Quote:
But at the end of the day it is my choice and i just don't think that i really need Boost right now. And you guys have posed to choices; Boost or POSIX, so i might end up using POSIX instead. But at least i know i have choices, and that there are ways for me do solve my problem.

The thing is the following: Boost has established itself as a semi-official supplement to the C++ Standard Library, and IMHO choosing it over other supplementary libraries should be natural when coding C++.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.