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

Started by
16 comments, last by demonkoryu 15 years, 9 months ago
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
Advertisement
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.
Quote:...but you might find boost.filesystem useful for this.
Yup, boost::filesystem is probably your best option here.
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
The Posix standard has some C functions guaranteed to be on pretty much any modern OS. They're pretty ugly though.
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.
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.
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).
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
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.

This topic is closed to new replies.

Advertisement