C++的Boost 文件系统库中有许多常用的函数可以帮助你进行文件和目录的操作。以下是一些常见的函数和它们的功能:
- boost::filesystem::create_directories():创建目录,包括中间目录(如果不存在的话)。
boost::filesystem::path dirPath("/path/to/directory");
boost::filesystem::create_directories(dirPath);
- boost::filesystem::create_directory():创建单个目录。
boost::filesystem::path dirPath("/path/to/directory");
boost::filesystem::create_directory(dirPath);
- boost::filesystem::remove():移除文件或目录。
boost::filesystem::path filePath("/path/to/file.txt");
boost::filesystem::remove(filePath);
boost::filesystem::path dirPath("/path/to/directory");
boost::filesystem::remove(dirPath);
- boost::filesystem::remove_all():递归地移除目录及其所有子目录和文件。
boost::filesystem::path dirPath("/path/to/directory");
boost::filesystem::remove_all(dirPath);
- boost::filesystem::exists():检查文件或目录是否存在。
boost::filesystem::path filePath("/path/to/file.txt");
if (boost::filesystem::exists(filePath)) {
// 文件存在
} else {
// 文件不存在
}
- boost::filesystem::is_directory():检查路径是否是一个目录。
boost::filesystem::path dirPath("/path/to/directory");
if (boost::filesystem::is_directory(dirPath)) {
// 是一个目录
} else {
// 不是一个目录
}
- boost::filesystem::directory_iterator:用于遍历目录中的文件和子目录。
boost::filesystem::path dirPath("/path/to/directory");
for (boost::filesystem::directory_iterator it(dirPath); it != boost::filesystem::directory_iterator(); ++it) {
if (boost::filesystem::is_regular_file(it->status())) {
// 处理文件
} else if (boost::filesystem::is_directory(it->status())) {
// 处理目录
}
}
这些是 Boost 文件系统库中一些常见的函数,它们可以帮助你执行各种文件和目录操作。
除了上面提到的常见函数外,Boost 文件系统库还提供了其他一些有用的函数。以下是其中一些:
- boost::filesystem::copy_file():复制文件。
boost::filesystem::path sourceFilePath("/path/to/source/file.txt");
boost::filesystem::path destinationFilePath("/path/to/destination/file.txt");
boost::filesystem::copy_file(sourceFilePath, destinationFilePath);
- boost::filesystem::rename():重命名文件或目录。
boost::filesystem::path oldFilePath("/path/to/old/file.txt");
boost::filesystem::path newFilePath("/path/to/new/file.txt");
boost::filesystem::rename(oldFilePath, newFilePath);
- boost::filesystem::file_size():获取文件大小。
boost::filesystem::path filePath("/path/to/file.txt");
std::uintmax_t fileSize = boost::filesystem::file_size(filePath);
- boost::filesystem::last_write_time():获取文件的最后修改时间。
boost::filesystem::path filePath("/path/to/file.txt");
std::time_t lastWriteTime = boost::filesystem::last_write_time(filePath);
- boost::filesystem::current_path():获取当前工作目录。
boost::filesystem::path currentPath = boost::filesystem::current_path();
- boost::filesystem::canonical():获取路径的规范形式。
boost::filesystem::path originalPath("/path/to/../file.txt");
boost::filesystem::path canonicalPath = boost::filesystem::canonical(originalPath);
这些是 Boost 文件系统库中的一些其他常用函数,它们提供了更多的文件和目录操作功能,可以满足更多的需求。