What's the C++ way of Perl's idiom:
my @files = glob("file*.txt");
foreach my $file (@files) {
# process $file
}
What's the C++ way of Perl's idiom:
my @files = glob("file*.txt");
foreach my $file (@files) {
# process $file
}
You can mimic the "glob" using fnmatch
. But you'll need to open the directory, read the contents, and match each entry using fnmatch
.
No direct equivalent that's standard AFAIK.
There's no standard C++ way to emulate this because there is no standard C++ functionality of reading the contents of a directory. What you can do is use Boost.Filesystem:
#include <boost/filesystem.hpp> // plus iostream,algorithm,string,iterator
using namespace boost::filesystem; // and std
struct pathname_of {
string operator()(const directory_entry& p) const {
return p.path().filename(); // or your creativity here
}
};
int main(int argc, char* argv[])
{
transform(directory_iterator("."), directory_iterator(),
ostream_iterator<string>(cout, "\n"),
pathname_of());
return 0;
}
The POSIX API specifies the glob()
and globfree()
functions for this. See the man page. wordexp()
and wordfree()
, also specified by POSIX, support other kinds of expansions as well.