views:

451

answers:

5

Hi there,

Is it possible in c++ to count the number of files with a given extension in a directory?

I'm writing a program where it would be nice to do something like this (pseudo-code):

if (file_extension == ".foo")
    num_files++;
for (int i = 0; i < num_files; i++)
    // do something

Obviously, this program is much more complex, but this should give you the general idea of what I'm trying to do.

If this is not possible, just tell me.

Thanks!

A: 

This example lists files in a directory, it could be modified to fit your needs.

GrayWizardx
A: 

Yes, it is possible.

frunsi
-1 for not giving an answer, and also giving wrong answer -- C++ does not provide directory listing functions.
Artyom
@Artyom, you can't have it both ways. Either it's not an answer or it's a wrong answer :-)
paxdiablo
Well, yes, I was mad about another person, which has no relation to this question. And then I read "Is it possible in c++ [..]" [..] "If this is not possible, just tell me.". And I thought well, if you want to learn something, then at least learn to ask questions. But well, ok, this is not usenet ;) This question became some kind of valve for me. Its not fine, but still the answer is a correct answer for the question (it is possible, point).
frunsi
+6  A: 

This kind of functionality is OS-specific, therefore there is no standard, portable method of doing this.

However, using Boost's Filesystem library you can do this, and much more file system related operations in a portable manner.

Ramon Zarazua
A: 

First of all what OS are you writing for?

  • If it is Windows then look for FindFirstFile and FindNextFile in MSDN.
  • If you are looking the code for POSIX systems, read man for opendir and readdir or readdir_r.
  • For cross platform I'd suggest using Boost.Filesystem library.
Artyom
+2  A: 

There is nothing in the C or C++ standards themselves about directory handling but just about any OS worth its salt will have such a beast, one example being the findfirst/findnext functions or readdir.

The way you would do it is a simple loop over those functions, checking the end of the strings returned for the extension you want.

Something like:

char *fspec = findfirst("/tmp");
while (fspec != NULL) {
    int len = strlen (fspec);
    if (len >= 4) {
        if (strcmp (".foo", fspec + len - 4) == 0) {
            printf ("%s\n", fspec);
        }
    }
    fspec = findnext();
}

As stated, the actual functions you will use for traversing the directory are OS-specific.

For UNIX, it would almost certainly be the use of opendir, readdir and closedir. This code is a good starting point for that:

#include <dirent.h>

int len;
struct dirent *pDirent;
DIR *pDir;

pDir = opendir("/tmp");
if (pDir != NULL) {
    while ((pDirent = readdir(pDir)) != NULL) {
        len = strlen (pDirent->d_name);
        if (len >= 4) {
            if (strcmp (".foo", &(pDirent->d_name[len - 4])) == 0) {
                printf ("%s\n", pDirent->d_name);
            }
        }
    }
    closedir (pDir);
}
paxdiablo
In both cases above. Would a for(;;) loop not be neater?
Martin York
Matter of taste or style, I think, @Martin. I tend to use for loops for simple things (mostly the "for i = 1 to 10" sort) and use while for more complicated loops. But in either case, not really relevant to the question at hand.
paxdiablo