c++ 终于有办法跨平台访问文件系统了,虽然还是要借助boost。不多说,上代码
/** visitdir.cpp **/
#include <iostream> #include <boost/filesystem.hpp> using namespace boost::filesystem; using namespace std; void print_files(const path &full_path) { if (exists(full_path)) { directory_iterator item_begin(full_path); directory_iterator item_end; for (; item_begin != item_end; ++item_begin) { if (is_directory(*item_begin)) { cout << item_begin->path().native() << " [dir]" << endl; print_files(item_begin->path()); } else { cout << item_begin->path().native() << endl; } } } } int main(int argc, const char **argv) { if (argc < 2) { cerr << "Usage: " << argv[0] << " <path>" <<endl; return 1; } path full_path(argv[1]); print_files(full_path); return 0; }
编译:
g++ visitdir.cpp -o visitdir -lboost_filesystem -lboost_system