标准库学习-<filesystem>文件系统

<filesystem>头文件自 C++17 起引入,提供了方便的路径操作功能

目录

1. std::filesystem::path

2. std::filesystem::exists

3. std::filesystem::create_directory

4. std::filesystem::remove

5. std::filesystem::rename

6. std::filesystem::copy

7. std::filesystem::space

8. std::filesystem::current_path

9. std::filesystem::absolute

10. std::filesystem::canonical

11. std::filesystem::temp_directory_path


1. std::filesystem::path

std::filesystem::path 类用于表示文件系统路径,并提供多种操作路径的方法

(1) 构造函数

std::filesystem::path p("/path/to/file");

(2) 成员函数

  • string(): 将路径转换为 std::string
  • filename(): 获取文件名部分。
  • parent_path(): 获取父路径。
  • extension(): 获取文件扩展名。
  • replace_extension(const path& new_extension): 替换文件扩展名。
  • root_name(): 获取根名。
  • root_directory(): 获取根目录。
  • relative_path(): 获取相对路径。
  • is_absolute(): 检查路径是否是绝对路径。
  • is_relative(): 检查路径是否是相对路径。

(3) 示例

std::filesystem::path p("/path/to/file.txt");
std::cout << p.filename() << std::endl;  // 输出: file.txt
std::cout << p.parent_path() << std::endl;  // 输出: /path/to
std::cout << p.extension() << std::endl;  // 输出: .txt

2. std::filesystem::exists

检查文件或目录是否存在。

bool exists = std::filesystem::exists("/path/to/file");

3. std::filesystem::create_directory

创建目录。

bool success = std::filesystem::create_directory("/path/to/new_directory");

4. std::filesystem::remove

删除文件或空目录。

bool success = std::filesystem::remove("/path/to/file_or_directory");

5. std::filesystem::rename

重命名文件或目录

std::filesystem::rename("/path/to/old_name", "/path/to/new_name");

6. std::filesystem::copy

复制文件或目录。

std::filesystem::copy("/path/to/source", "/path/to/destination");

7. std::filesystem::space

获取磁盘空间信息。

std::filesystem::space_info info = std::filesystem::space("/path/to/directory");
std::cout << "Available: " << info.available << std::endl;
std::cout << "Free: " << info.free << std::endl;
std::cout << "Capacity: " << info.capacity << std::endl;

8. std::filesystem::current_path

获取或设置当前工作目录

获取:

std::filesystem::path current = std::filesystem::current_path();

设置:

std::filesystem::current_path("/new/current/directory");

9. std::filesystem::absolute

获取绝对路径

std::filesystem::path abs_path = std::filesystem::absolute("relative/path/to/file");

10. std::filesystem::canonical

获取规范路径(消除符号链接和冗余的 ...)。

std::filesystem::path can_path = std::filesystem::canonical("/path/to/file");

11. std::filesystem::temp_directory_path

获取临时目录路径。

std::filesystem::path temp_path = std::filesystem::temp_directory_path();

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
–mkdir: Unknown command Usage: hadoop fs [generic options] [-appendToFile <localsrc> ... <dst>] [-cat [-ignoreCrc] <src> ...] [-checksum <src> ...] [-chgrp [-R] GROUP PATH...] [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...] [-chown [-R] [OWNER][:[GROUP]] PATH...] [-copyFromLocal [-f] [-p] [-l] [-d] <localsrc> ... <dst>] [-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...] [-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>] [-createSnapshot <snapshotDir> [<snapshotName>]] [-deleteSnapshot <snapshotDir> <snapshotName>] [-df [-h] [<path> ...]] [-du [-s] [-h] [-x] <path> ...] [-expunge] [-find <path> ... <expression> ...] [-get [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>] [-getfacl [-R] <path>] [-getfattr [-R] {-n name | -d} [-e en] <path>] [-getmerge [-nl] [-skip-empty-file] <src> <localdst>] [-help [cmd ...]] [-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]] [-mkdir [-p] <path> ...] [-moveFromLocal <localsrc> ... <dst>] [-moveToLocal <src> <localdst>] [-mv <src> ... <dst>] [-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>] [-renameSnapshot <snapshotDir> <oldName> <newName>] [-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...] [-rmdir [--ignore-fail-on-non-empty] <dir> ...] [-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]] [-setfattr {-n name [-v value] | -x name} <path>] [-setrep [-R] [-w] <rep> <path> ...] [-stat [format] <path> ...] [-tail [-f] <file>] [-test -[defsz] <path>] [-text [-ignoreCrc] <src> ...] [-touchz <path> ...] [-truncate [-w] <length> <path> ...] [-usage [cmd ...]] Generic options supported are: -conf <configuration file> specify an application configuration file -D <property=value> define a value for a given property -fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations. -jt <local|resourcemanager:port> specify a ResourceManager -files <file1,...> specify a comma-separated list of files to be copied to the map reduce cluster -libjars <jar1,...> specify a comma-separated list of jar files to be included in the classpath -archives <archive1,...> specify a comma-separated list of archives to be unarchived on the compute machines The general command line syntax is: command [genericOptions] [commandOptions]
06-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值