一、基本介紹
boost::filesystem庫提供了一個現代化、面向對象的C++庫,用於處理文件系統中的文件和目錄。它提供了一組簡單的類和函數,讓我們能夠方便地進行文件和目錄的遍歷、查找、複製和刪除等操作。
二、開發環境及編譯安裝
安裝boost::filesystem需要先安裝boost庫。我們可以從boost的官方網站https://www.boost.org/下載源代碼,解壓後進入源代碼目錄,運行以下命令:
./bootstrap.sh ./b2
其中,bootstrap.sh在Linux/macOS下執行,對於Windows環境下,請使用bootstrap.bat。
如果在安裝boost時只需要安裝boost::filesystem,那麼我們可以使用以下命令:
./b2 --with-filesystem
三、文件和目錄的創建
boost::filesystem庫中提供了方便的函數,用於創建文件和目錄。
1、創建目錄
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path dir("/Users/test");
bool success = boost::filesystem::create_directory(dir);
if(success)
std::cout<<"目錄創建成功"<<std::endl;
else
std::cout<<"目錄創建失敗"<<std::endl;
return 0;
}
2、創建文件
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path file("/Users/test.txt");
std::ofstream ofs(file.string());
if(ofs)
std::cout<<"文件創建成功"<<std::endl;
else
std::cout<<"文件創建失敗"<<std::endl;
ofs.close();
return 0;
}
四、文件和目錄的遍歷
boost::filesystem庫提供了遍歷文件和目錄的函數,我們可以很方便地獲取文件和目錄列表。
1、列出當前目錄下的文件和目錄
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path dir(".");
boost::filesystem::directory_iterator it(dir), end;
while(it != end)
{
std::cout <path() << std::endl;
++it;
}
return 0;
}
2、列出指定目錄下的文件和目錄
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path dir("/Users");
boost::filesystem::directory_iterator it(dir), end;
while(it != end)
{
std::cout <path() << std::endl;
++it;
}
return 0;
}
五、文件和目錄的複製和刪除
boost::filesystem庫提供了方便的函數,用於文件和目錄的複製和刪除操作。
1、複製文件
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path file1("/Users/test.txt");
boost::filesystem::path file2("/Users/test_copy.txt");
boost::filesystem::copy_file(file1, file2);
std::cout<<"文件複製成功"<<std::endl;
return 0;
}
2、複製目錄
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path dir1("/Users/test");
boost::filesystem::path dir2("/Users/test_copy");
boost::filesystem::copy_directory(dir1, dir2);
std::cout<<"目錄複製成功"<<std::endl;
return 0;
}
3、刪除文件
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path file("/Users/test.txt");
bool success = boost::filesystem::remove(file);
if(success)
std::cout<<"文件刪除成功"<<std::endl;
else
std::cout<<"文件刪除失敗"<<std::endl;
return 0;
}
4、刪除目錄
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path dir("/Users/test");
bool success = boost::filesystem::remove_all(dir);
if(success)
std::cout<<"目錄刪除成功"<<std::endl;
else
std::cout<<"目錄刪除失敗"<<std::endl;
return 0;
}
六、使用正則表達式查找文件
boost::filesystem庫中的正則表達式函數,可以方便地找到文件名匹配某個模式的文件列表。
#include <boost/filesystem.hpp>
#include <regex>
#include <iostream>
int main()
{
boost::filesystem::path dir(".");
std::regex reg("test.*");
boost::filesystem::directory_iterator it(dir), end;
while(it != end)
{
std::string filename = it->path().filename().string();
if(std::regex_match(filename, reg))
std::cout<path()<<std::endl;
++it;
}
return 0;
}
七、總結
boost::filesystem庫提供了一組簡單易用的類和函數,讓我們能夠方便地進行文件和目錄的遍歷、查找、複製和刪除等操作。我們可以在項目中使用這個庫來操作文件系統,為我們的開發工作帶來便利。
原創文章,作者:ESQLL,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/367992.html
微信掃一掃
支付寶掃一掃