tags:

views:

159

answers:

3

How do I get the total number of files in a directory by using C++ standard library? Any help is appreciated.

+10  A: 

You can't. The closest you are going to be able to get is to use something like Boost.Filesystem

Yacoby
After which it's trivial: `int count = std::difference(directory_iterator(dir_path), directory_iterator());`
MSalters
@MSalters, I can't find any reference to `std::difference`. Are you sure you didn't mean `std::distance`? Also, you'll need a `static_cast<int>` to coerce `directory_iterator::difference_type` to `int`.
Nathan Ernst
I'm trying to avoid linking to Boost libraries and use only headers for algorithms.
harik
@yacoby, do we have a way to avoid linking to Boost filesystem library and still get the support for this?
harik
@Nathan: of course. @harik: doesn't work; the code you need is in the Boost library.
MSalters
+2  A: 

You would need to use a native API or framework.

Steven Jackson
+4  A: 

If you don't exclude the basically always available C standard library, you can use that one. Because it's available everywhere anyways, unlike boost, it's a pretty usable option!

An example is given here.

And here:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main (void)
{
  DIR *dp;
  int i;
  struct dirent *ep;     
  dp = opendir ("./");

  if (dp != NULL)
  {
    while (ep = readdir (dp))
      i++;

    (void) closedir (dp);
  }
  else
    perror ("Couldn't open the directory");

  printf("There's %d files in the current directory.\n", i);

  return 0;
}

And sure enough

 > $ ls -a | wc -l
138
 > $ ./count
There's 138 files in the current directory.

This isn't C++ at all, but it is available on most, if not all, operating systems, and will work in C++ regardless.

UPDATE: I'll correct my previous statement about this being part of the C standard library - it's not. But you can carry this concept to other operating systems, because they all have their ways of dealing with files without having to grab out additional libraries.

LukeN
-1: The C standard library provides no way to enumerate a directory either.
Billy ONeal
That's a stupid down vote. Of course you can count files using the C standard lib. Take the example I included in my post - instead of calling a "puts ..." inside the while used to iterate over every file, just do "i++" and declare an "int i" somewhere above. There is of course no "directory_get_file_count" function, but that isn't the point. The point is, you CAN use it to get your desired result, namely the amount of files in a folder. The hell, let me edit my original answer with a spoon fed answer, sec
LukeN
-1, `readdir` is POSIX not standard C.
MSalters
+1, i agree Luke
espais
As for the POSIX part of readdir, that's true, but in windows you have similar functions that I could not test here because I haven't used windows in ages. But the point is: this works in C++, this works in C, you don't need to install special library support and that's what the OP probably wanted.
LukeN
@LukeN: POSIX **is** "special library support". Windows does not have a `<sys/types.h>` nor does it have a `<dirent.h>`. POSIX is not the C standard library, and your assertion that it is is wrong.
Billy ONeal
POSIX yay or nay left aside, one would not simply transfer a C program like this from one operating system to another anyways - all major operating systems have their ways to deal with files and directories. THIS was the Linux example, windows deal with it another way, probably windows.h or something, one can just write some "#ifdef" to handle that and be happy. I'll correct my statement about it being standard C, and just say that those functions will be available wherever such a program would typically run. The asker does not have to install a special library, and that's what counts for me!
LukeN
I tend to take things like this to personal, sorry about that. Of course you're right. But I think this is what the poster wanted. Listing files without separatly installing libraries like boost. :)
LukeN
Basically you've now arrived at the comment Daniel made initially: "just use the native OS functions, not a (standard) library".
MSalters
Basically, but I supplied examples, too!
LukeN
This solution will not work for me on Windows. I may have to use Boost like libraries for getting required support across platforms.
harik