boost filesystem测试
2016-01-18 16:18
411 查看
/* Title:boost filesystem 子库测试 Author:kagula Date:2016-01-18 Environment:VS2013Update5 boost 1.57 Prologue: 为了使今后的代码更有可移植性测试下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.exe std::cout << dir.parent_path() << std::endl; //上级目录:C:\Windows\System32 std::cout << dir.filename() << std::endl; //文件名:services.exe std::cout << dir.stem() << std::endl; //不带扩展的文件名:services std::cout << dir.extension() << std::endl; //扩展名:.exe } } // recursively copy file or directory from $src to $dst void 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::cout namespace fs = boost::filesystem; // 宏FSTEST:测试f的成员函数,输出成员函数名和结果 #define FSTEST(x) std::cout << #x##": " << f.x << std::endl int 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::endl FSTEST2(initial_path());//得到程序运行时的系统当前路径 FSTEST2(current_path());//得到系统当前路径 //要得到EXE文件所在的路径或AppData路径,只能通过调用Win API实现。 return 0; }
相关文章推荐
- PHP移动文件或者文件夹
- Handler类的简单使用与进度条的更新
- java解压缩文件的实现示例,支持rar和zip格式 ____http://my.oschina.net/manville/blog/280420
- 【机器学习算法】之Apriori
- XML DOM解析 基础概念
- 程序员常见面试之 数据库 知识点小结(三)
- Java 多线程(七) 线程间的通信——wait及notify方法
- win7怎么设置麦克风?win7设置麦克风的方法
- (assign,retain,copy):
- sqlserver的最大连接数测试
- C4.5算法
- 系统调用基本概念
- pb web 环境配置及常见错误处理
- 前闭后开区间表示法
- zxing.dll生成条码
- 【BZOJ 2152】 聪聪可可
- Gradle DSL method not found: 'compile() 解决办法
- iOS关于导航设置的问题
- 一到周末就惰性,这要改啊要改啊
- Hyper-V 角色“操作定义”翻译