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() << "\t[dir]" << endl;
print_files(item_begin->path());
}
else
{
cout << item_begin->path().native() << endl;
}
}
}
} int main(int argc, const char **argv)
{
if (argc < )
{
cerr << "Usage: " << argv[] << " <path>" <<endl;
return ;
} path full_path(argv[]);
print_files(full_path);
return ;
}
编译:
g++ visitdir.cpp -o visitdir -lboost_filesystem -lboost_system