#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <ctime>
#pragma warning(push) //仅禁用此头文件
#pragma warning(disable:4996)
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#pragma warning(pop) //恢复最初的警告级别
#pragma comment(lib, "libboost_filesystem-vc140-mt-sgd-x32-1_70.lib")
//库Boost.Filesystem简化了处理文件和目录的工作。
//它提供了一个名为boost::filesystem::path的类,可以对路径进行处理。
//另外,还有多个函数用于创建目录或验证某个给定文件的有效性。
int _tmain(int argc, _TCHAR* argv[])
{
//boost::filesystem::path 的构造函数同时支持可移植路径和平台相关路径
//创建路径
boost::filesystem::path p1("C:\\");
boost::filesystem::path p2("C:\\Windows");
boost::filesystem::path p3("C:\\Windows\\System");
std::cout << p3.string() << std::endl; //string()方法返回一个所谓的可移至路径
std::cout << p3.root_name() << std::endl; //根目录 C:
std::cout << p3.root_directory() << std::endl; // 根 '\'
std::cout << p3.root_path() << std::endl; //根目录 “C:\”
std::cout << p3.relative_path() << std::endl; //“Windows\System”
std::cout << p3.parent_path() << std::endl; //父目录“C:\Windows”
std::cout << p3.filename() << std::endl; //文件名“System”
boost::filesystem::path p4("photo.jpg");
std::cout << p4.stem() << std::endl; //photo
std::cout << p4.extension() << std::endl; //.jpg
std::cout << "------------------------------" << std::endl;
for (boost::filesystem::path::iterator it = p3.begin(); it!=p3.end(); ++it)
{
std::cout << *it << " "; //"C:" "/" "Windows" "System"
}
std::cout << std::endl;
std::cout << "------------------------------" << std::endl;
//重载operator/=()操作符,讲一个路径添加到另一个之上
p1 /= "Windows\\System";
std::cout << p1.string() << std::endl;
//==================文件与目录操作==========================
//查询一个文件或目录状态
boost::filesystem::path p("C:\\");
try
{
boost::filesystem::file_status s = boost::filesystem::status(p);
std::cout << boost::filesystem::is_directory(s) << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//以字节数返回一个文件大小
boost::filesystem::path pp("C:\\Windows\\win.ini");
try
{
std::cout << boost::filesystem::file_size(pp) << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//获取一个文件最后被修改的时间
try
{
std::time_t t = boost::filesystem::last_write_time(p);
std::cout << std::ctime(&t) << std::endl;
}
catch (boost::filesystem::filesystem_error &e)
{
std::cerr << e.what() << std::endl;
}
//获取磁盘的总空间和剩余空间
try
{
boost::filesystem::space_info s = boost::filesystem::space(p);
std::cout << s.capacity / 1024 / 1024 / 1024 << "GB" << std::endl;
std::cout << s.free / 1024 / 1024 / 1024 << "GB" << std::endl;
std::cout << s.available / 1024 / 1024 / 1024 << "GB" << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//创建、修改、重命名和删除文件和目录
boost::filesystem::path pTest("C:\\Test");
try
{
if (boost::filesystem::create_directory(p))
{
boost::filesystem::rename(p, "C:\\Test2");
boost::filesystem::remove("C:\\Test2");
}
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//基于一个文件名或一小节路径来创建一个绝对路径
try
{
//输出路径由该程序运行时所处的路径决定。如果从C:\运行,输出C:/photo.jpg
std::cout << boost::filesystem::complete("photo.jpg") << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//要取出一个相对于其它目录的绝对路径,可将其传递给第二个参数
try
{
std::cout << boost::filesystem::complete("photo.jpg", "D:\\") << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//获取当前工作目录
try
{
//函数 boost::filesystem::initial_path() 用于返回应用程序开始执行时所处的目录
std::cout << boost::filesystem::initial_path() << std::endl;
std::cout << boost::filesystem::current_path() << std::endl;
}
catch (boost::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
//==================文件流操作==========================
boost::filesystem::path pTestFile("test.txt");
boost::filesystem::ofstream ofs(pTestFile);
ofs << "Hello, world!" << std::endl;
return 0;
}