Traversing a directory tree in C++ - c++

This is been a curiosity of mine for a while: how do you traverse a directory tree without using boost or any third-party library? Just plain ol' C++ (examples in 98, 99, 01, 0x and 1x specs are okay.)? It was done back in the day before boost existed so there's got to be a way to do it.

Please take a look at http://en.wikipedia.org/wiki/Dirent.h
The reference also has a link to dirent.h implementation for Windows or you can use cygwin
If you want to just do it for Windows you can build upon this example
http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx

There are no standard filesystem functions, so you won't get any answers that use "plain C++". For POSIX systems, opendir is used. For Windows, FindFirstFile. I'm not sure about other OSes.
There's a reason people recommend Boost Filesystem—it's portable and takes care of all these details for you.

As of the adoption of the C++17 standard there exists a <filesystem> header included in the language that does exactly this. See your compiler's documentation to determine if it's supported.

Related

Clear directory in standard C++ (cross-platform)

My goal is simple: I'd like a to clear a directory (delete all of the sub-directories and files it contains, but not delete the directory itself) given a path, in a cross-platform way.
Most of the solutions I've found online either involve using dirent.h which, from my understanding, is non-standard and may not work on non-POSIX systems (notably Windows) or using Boost.Filesystem. However, building Boost and including it in my project is a lot to ask for if all I'd like to do is clear a directory.
Is there a way to achieve my goal in standard C++? Or, has the standard not advanced to this point yet?
A filesystems library was "added" to C++ in the "Filesystems TS", so you may be able to find an experimental implementation in your compiler's standard library implementation.
However, it's not yet part of any formal standard. My understanding is that it'll be part of C++17.
Until then, Boost it is — or your own hand-crafted code on systems that are neither Windows nor POSIX-compliant.

Scanning files in C++ using its own standard libraries

I want to know, or get a clue how to search through a directory in C++ with its own standard libraries or to see if it is possible at all.
Currently I have found several solution to searching directory, but they all use some external libraries such as this SO question:
recursive folder scanning in c++
How should we approach this?
There is no filesystem facilities in the C++ standard. There is a technical specification for it which may be included in a future version of C++.
Until then, you will have to use platform specific functions or a library that wraps them. Apparently the new proposal is almost the same as boost's API, so I recommend using boost even if it's ubiquitousness wasn't good enough reason already.

Program option library for portable code

I have a portable code running on Visual C++ 2008 and RHEL 5.3 (gcc 4.x.x).
My program should accept command line arguments. I consider using some library for that task.
My candidats are:
Boost program options
ACE has this capability too
(1) is not in standard and as for (2) we already using it heavily for other tasks.
Which is prefered one? Maybe there're other libraries out there?
I like a lot boost::PO, but I never used ACE, so I can't compare.
You're saying that boost is not a standard, but is it really a problem? Many people consider it as almost a standard. At least it isn't any exotic library.
Personally, I'd just use getopt.h on *nix and include something like http://doxygen.postgresql.org/getopt_8c-source.html in the build on windows.
Writing your own can be an option as well. It's not that hard of a problem to solve.
But if you're already using one of the libraries then that is the most obvious choice.

Reading file names with C++

Is there a way to read file names from a folder using purely C (or C++)? That means without including windows.h (no FindFirstFile(), etc...).
It doesn't look like fstream has this functionality. I know that file names are operating system dependent, but I was hoping there is some library that will allow it in Windows.
boost filesystem is a nice solution. Of course under the hood, it will still be using the windows API calls (when you build on windows), but this is abstracted away from you.
C++ typically does not supply you with such functionality. A cross-platform solution is to use boost::filesystem.
Try the POSIX functions opendir() and readdir() for iterating through directories. See this link for the manual page with some great example code. These functions should be available on most platforms, both Windows and UNIX.
If you wish to use opendir() and readdir() on windows, you can download MinGW, a windows port of the famous GNU compiler collection. It includes windows ports of the UNIX header files, including dirent.h, which will allow you to use the specified functions. Keep in mind these will call native API's either way.
-John

Is there a standard way to do findfirst, findnext with gcc on linux using stl?

I can't seem to find the _findfirst / findfirst, _findnext / findnext API on gcc for Linux, and would actually rather use the Standard Template Library (STL) for that if it is included there.
Does anyone know what API there is available for listing files in a directory under Linux for C++ (gcc)?
It's not a C++-style API, but the API you aren't finding (the Linux/Unix correspondent of DOS/Windows-style findfirst/findnext) is opendir/readdir/closedir.
The main advantage of using opendir/readdir/closedir is that you do not need any extra library (it's part of the C library, which you are already using). In fact, the Boost filesystem library uses opendir/readdir/closedir to get the list of files in a directory.
References:
http://www.opengroup.org/onlinepubs/009695399/functions/opendir.html
http://www.opengroup.org/onlinepubs/009695399/functions/readdir.html
http://www.opengroup.org/onlinepubs/009695399/functions/closedir.html
Check out the Boost.Filesystem library.
In particular, the basic_directory_iterator.
The STL does not, yet, have functions for listing files in a directory. But it does have functions for opening files you are already aware of.
Aside from Boost.Filesystem, there is also STLSoft
Since C++17 the standard library contains std::filesystem which has its source in Boost.Filesystem. Nowadays std::filesystem::directory_iterator is the obvious choice since it is platform-independent, offers better abstraction than _findfirst/findnext/opendir/readdir/closedir and doesn't introduce any dependencies. If you can't use a C++17-compliant compiler use Boost for now and switch over later.