boost filesystem测试

来源:互联网 发布:淘宝 app 编辑:程序博客网 时间:2024/05/29 11:15
/*Title:boost filesystem 子库测试Author:kagulaDate:2016-01-18Environment:VS2013Update5 boost 1.57Prologue:为了使今后的代码更有可移植性测试下boost filesystem。Reference:[1]《(九)boost库之文件处理filesystem》http://my.oschina.net/lingluonianhua/blog/213999[2]http://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/index.htm*/#include <boost/filesystem.hpp>#include <iostream>using namespace std;void TestCreate(){//若目录已经存在,也不会抛出异常。boost::filesystem::create_directories("d:/temp");}void TestDelete(){try{//删除文件{boost::filesystem::path filename;filename = "D:/temp/Launcher.exe";remove(filename);//如果filename变量存放的是dir,会抛出异常.}//删除目录(非空folder也会被删除){boost::filesystem::path dir;dir = "D:/temp";remove_all(dir);}}catch (boost::filesystem::filesystem_error& /*e*/){}}void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst);void TestModify(){try{//copy_directory老是失败,原来它是create directory功能,方法名造成了误导!//真实的意思是创建tempDest目录,这个目录的属性来自tempSrc目录。//boost::filesystem::copy_directory("D:/tempSrc/", "d:/tempDest");//下面的代码调用失败会throw exception.//boost::filesystem::rename("d:/temp", "d:/temp2");//"D:/apache-activemq-5.13.0"目录下的文件复制到“d:/temp2”目录下.CopyFiles("D:/apache-activemq-5.13.0", "d:/temp2");}catch (boost::filesystem::filesystem_error& e){std::cout << e.path1() << std::endl;std::cout << e.what() << std::endl;}}void TestQuery(){try{boost::filesystem::path dir("c:\\Windows\\System32");boost::filesystem::path file("D:/temp/Launcher.exe");//是文件吗?cout << dir.string() << "=>is_regular_file=>" << (boost::filesystem::is_regular_file(dir) ? "true" : "false") << endl;cout << file.string() << "=>is_regular_file=>" << (boost::filesystem::is_regular_file(file) ? "true" : "false") << endl;//是目录吗cout << dir.string() << "=>is_directory=>" << (boost::filesystem::is_directory(dir) ? "true" : "false") << endl;cout << file.string() << "=>is_directory=>" << (boost::filesystem::is_directory(file) ? "true" : "false") << endl;//文件是否存在,目录是否存在cout << dir.string() << "=>exists=>" << (boost::filesystem::exists(dir) ? "true" : "false") << endl;cout << file.string() << "=>exists=>" << (boost::filesystem::exists(file) ? "true" : "false") << endl;cout << "枚举(本层)目录中的所有文件" << endl;{boost::filesystem::path dir2("c:\\Windows\\System32");boost::filesystem::directory_iterator end;for (boost::filesystem::directory_iterator pos(dir2); pos != end; pos++){std::cout << *pos << std::endl;}}cout << "枚举目录中的所有文件" << endl;{typedef boost::filesystem::recursive_directory_iterator rd_iterator;boost::filesystem::path dir2("D://apache-activemq-5.13.0");rd_iterator end;for (rd_iterator pos(dir2); pos != end; pos++){//如果深度大于4层,则不再继续深入if (is_directory(*pos) && pos.level() > 4){pos.no_push();}//如果该目录下有nofind.txt文件,则跳出该目录if (*pos == "nofind.txt"){pos.pop();}std::cout << *pos << std::endl;}}}catch (boost::filesystem::filesystem_error& e){std::cout << e.path1() << std::endl;std::cout << e.what() << std::endl;}cout << "测试路径信息" << endl;{boost::filesystem::path dir("C:\\Windows");dir /= "System32";       //追加下级目录dir /= "services.exe";std::cout << dir << std::endl;std::cout << dir.string() << std::endl;             //转换成std::string 类型std::cout << dir.root_name() << std::endl;          //盘符名:C:std::cout << dir.root_directory() << std::endl;     //根目录:"\"std::cout << dir.root_path() << std::endl;          //根路径:"C:\"std::cout << dir.relative_path() << std::endl;      // 相对路径:Windows\System32\services.exestd::cout << dir.parent_path() << std::endl;        //上级目录:C:\Windows\System32std::cout << dir.filename() << std::endl;           //文件名:services.exestd::cout << dir.stem() << std::endl;               //不带扩展的文件名:servicesstd::cout << dir.extension() << std::endl;          //扩展名:.exe}}// recursively copy file or directory from $src to $dstvoid CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst){if (!boost::filesystem::exists(dst)){boost::filesystem::create_directories(dst);}for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it){const boost::filesystem::path newSrc = it->path();const boost::filesystem::path newDst = dst / it->path().filename().string();if (boost::filesystem::is_directory(newSrc)){CopyFiles(newSrc, newDst);}else if (boost::filesystem::is_regular_file(newSrc)){boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);}else{cerr << "Error: unrecognized file - " << newSrc.string() << endl;}}}int main(int argc, char *argv[]){cout << "boost 1.57" << endl;//TestCreate();TestModify();//TestDelete();//TestQuery();cin.get();return 0;}


附对宏的很有意思的调用

#include "boost/filesystem.hpp"   // 包含所有需要的 Boost.Filesystem 声明#include <iostream>               // 使用 std::coutnamespace fs = boost::filesystem;// 宏FSTEST:测试f的成员函数,输出成员函数名和结果#define FSTEST(x) std::cout << #x##": " << f.x << std::endlint main(int argc, char **argv){fs::path f("\\folder1\\folder2\\folder3\\filename.ext");FSTEST(string());FSTEST(root_name());FSTEST(root_directory());FSTEST(root_path());FSTEST(relative_path());FSTEST(filename());FSTEST(parent_path());FSTEST(stem());FSTEST(extension());FSTEST(replace_extension("new"));char buf[] = "hello";FSTEST(append(buf, buf + sizeof(buf)));FSTEST(remove_filename());//#define  FSTEST2(x) std::cout << #x##": " << fs::x << std::endlFSTEST2(initial_path());//得到程序运行时的系统当前路径FSTEST2(current_path());//得到系统当前路径//要得到EXE文件所在的路径或AppData路径,只能通过调用Win API实现。return 0;}


 

0 0