一、VS2019 安装配置boost库
1、库下载
boost v1.75下载: Boost Downloads
boost v1.70下载:Version 1.70.0
本文使用boost v1.70 ,vs使用2019
解压boost库
2、使用VS tool中developer Command终端工具对解压之后的文件boostrap.bat进行编译
进入解压后的boost目录,执行 bootstrap.bat 命令
生成如下两个文件:
在命令行终端执行如下命令:注意vs2019对应 14.2
bjam.exe --toolset=msvc-14.2 architecture=x86 address-model=64 link=static --build-type=complete --with-system --with-thread --with-date_time --with-filesystem --with-serialization
3、vs2019 配置环境
新建vs2019 C++项目,并添加boost依赖库 >点击菜单栏 项目——>属性——>选择VC++目录,在包含目录 和 库目录添加以上下2个路径:
配置安装完成。
二、C++ 文件读写
2.1 文件目录及创建 boost::filesystem
boost::filesystem是Boost C++ Libraries中的一个模块,主要作用是处理文件(Files)和目录(Directories)。
使用头文件
#include "boost/filesystem.hpp"
boost::filesystem::create_directory(''/root/mytest") ; 创建目录
boost::filesystem::path myPath = ''/root/mytest";
boost::filesystem::exists() ; 判断是否存在
boost::filesystem::create_directory() 创建目录
应用举例:
#include <iostream>
#include <functional>
#include "boost/filesystem.hpp"
int main()
{
boost::filesystem::path path("/test/test1"); //初始化
boost::filesystem::path old_cpath = boost::filesystem::current_path(); //取得当前程序所在文件夹
boost::filesystem::path parent_path = old_cpath.parent_path();//取old_cpath的上一层父文件夹路径
boost::filesystem::path file_path = old_cpath / "file"; //path支持重载/运算符
if (boost::filesystem::exists(file_path)) //推断文件存在性
{
std::string strPath = file_path.string();
int x = 1;
}
else
{
//文件夹不存在;
boost::filesystem::create_directory(file_path); //文件夹不存在。创建
}
boost::filesystem::path new_file_path = file_path / "test.txt";
if (boost::filesystem::is_regular_file(new_file_path)) //推断是否为普通文件
{
auto sizefile = boost::filesystem::file_size(new_file_path); //文件大小(字节)
std::cout << "file size = " << sizefile;
}
}
2.2 写文件 std::ofstream
在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的。
在C++中,对文件的操作是通过stream的子类fstream(file stream)来实现的。文件有关系的输入/输出类主要在头文件<fstream>中被定义。在这个头文件中主要定义了3个类。由这3个类控制对文件的各种输 入/输出操作,它们分别是ifstream、ofstream、fstream。其中基类是fstream
,而ifstream
是对于输入特性进行拓展的继承类。ofstream
就是对于输出特性进行拓展的继承类。
2.1 打开文件
使用fstream类中的成员函数open()
void open(const char* filename,int mode,int access);
filename: 要打开的文件名
mode: 要打开文件的方式
ofstream 默认是以输出方式打开文件,若文件不存在,则自动创建
access: 打开文件的属性打开文件的方式
// ofstream constructor.
#include <fstream> // std::ofstream
int main () {
std::ofstream ofs ("test.txt", std::ofstream::out);
ofs << "lorem ipsum";
ofs.close();
return 0;
}
指定路径下,写文件,采用覆盖的策略:
std::string fileName =/bin/test.txt ;
try {
std::ofstream ofstr(fileName);//文件不存在则创建
if (!ofstr.is_open()) {
cout <<"error"<<endl;
return false;
}
ofstr << "写入内容" << '\n';
ofstr.close();
}
catch(...)
{
}
写二进制文件流
sfileName = "123.bin";
std::ofstream ofstr(sfileName, std::ofstream::out | std::ios_base::binary);
if (!ofstr.is_open()) {
}
char *data ="qsasssl\n";
ofstr.write(data, sizeof(char)*7);
ofstr.close();
2.3 读文件 std::ifstream
std::string path = "/test.txt";
std::string temp;
std::ifstream infile(path);
if(!infile.is_open())
{
std::cout << "open error";
}
while(getline(infile,temp))
{
}
infile.close();
参考文献:
【1】boost::filesystem::path 用法:boost::filesystem总结 - Lilu1223 - 博客园
【2】vs2019 安装boost库: VS2019配置C++ boost库 - 牛犁heart - 博客园
【3】boost::filesystem经常使用使用方法具体解释 :boost::filesystem经常使用使用方法具体解释 - yxysuanfa - 博客园