Search code examples
c++visual-c++visual-c++-2010-express

replacement for findfirst() and findnext()


Is there any replacement for findfirst() and findnext(). I am using microsoft visual c++ 2010 express and it doesn't support these functions neither the header file <dir.h> ?

I was looking to count the number of files in the directory using these functions but i am having a problem without these functions.

If there is no replacement of the mentioned functions is there any other way out. ? Some other functions ?


Solution

  • As 'iammilind' said in the comments (probably worthy of an answer) - you can use the windows api's FindFirstFile and FindNextFile functions, you just have to fill up a struct and iterate through the latter until you reach an invalid handle. These functions do work on console, but you must include the 'Windows.h' header.

    These functions do come with a couple of pitfalls however, and if you want your code to run on anything other than windows you're probably better off using another header/library (such as Boost::Filesystem, mentioned by vBx).

    Also, this may be of help: C++ - Load all filename + count the number of files in a current directory + filter file extension