1

I'm looking for a function that will return a list of what is in a particular directory. The closest that I have gotten is by using this:

system("dir");

But this only prints the contents of the working directory, and I can't CD to anywhere else.

I'm using windows, and I have no plans to make it cross-platform, so don't worry about that.

Magicaxis
  • 371
  • 7
  • 16
  • 3
    Either use `boost::filesystem` or a combination of `FindFirstFile` and `FindNextFile` from the winapi comes to mind. Any preferences? – Jesse Good May 14 '12 at 03:54
  • 1
    On POSIX compliant systems you can use opendir and related functions - http://www.kernel.org/doc/man-pages/online/pages/man3/opendir.3.html –  May 14 '12 at 03:57
  • 1
    possible duplicate of [How can I get a list of files in a directory using C or C++?](http://stackoverflow.com/questions/612097/how-can-i-get-a-list-of-files-in-a-directory-using-c-or-c) – Adam Rosenfield May 14 '12 at 04:31
  • I didn't see that one before I posted this one, but that accepted answer is also pretty dayum good :) – Magicaxis May 14 '12 at 04:58

1 Answers1

2

Look at the following example directly copied from this page. It uses boost::filesystem so works on all major systems.

int main(int argc, char* argv[])
{
    path p (/* Specify a directory here */);

    try
    {
        if (exists(p))    // does p actually exist?
        {
            if (is_regular_file(p))        // is p a regular file?   
                cout << p << " size is " << file_size(p) << '\n';
            else if (is_directory(p))      // is p a directory?
            {
                cout << p << " is a directory containing:\n";

                copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
                  ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is
                                                                  // converted to a path by the
                                                                  // path stream inserter
            }
            else
                cout << p << " exists, but is neither a regular file nor a directory\n";
        }
        else
            cout << p << " does not exist\n";
    }

    catch (const filesystem_error& ex)
    {
        cout << ex.what() << '\n';
    }

    return 0;
}
Hindol
  • 2,924
  • 2
  • 28
  • 41
  • That's what I'm looking for :3 just one important bit I don't understand is what argv[1] is. Through uni all they told me was "You can put it as an arguement for main, but it doesnt really do anything so don't bother". I now assume it has some purpose lol. – Magicaxis May 14 '12 at 04:15
  • 1
    @Magicaxis Wait I'll modify my answer. BTW, say your executable is `listDir.exe` and in the command line you enter `listDir.exe C:\ExampleDir` then `argv[0] = listDir.exe` and `argv[1] = C:\ExampleDir` and `argc = 2`. Google `command line arguments in c / c++` and you'll know more, :). And if this is your answer then mark it. – Hindol May 14 '12 at 04:20
  • 1
    See also [C++ - int main(int argc, char **argv)](http://stackoverflow.com/q/5217395/78845) on this site :) – johnsyweb May 14 '12 at 04:30