What's the C++ way of Perl's idiom:
my @files = glob("file*.txt");
foreach my $file (@files) {
# process $file
}
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With