您的位置:首页 > 其它

boost的filesystem使用示例

2017-01-13 14:24 295 查看
#include "boost/filesystem.hpp"   // 包含所有需要的 Boost.Filesystem 声明
#include <iostream>               // 使用 std::cout
#include <string>
#include <sstream>
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实现。

{
std::cout << "convert to std::string()=>" << fs::initial_path().string();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  boost filesystem