VS2017 C++ filesystem

VS2017 C++ 还没有正式支持filesystem,命名空间是std::experimental::filesystem。

https://zh.cppreference.com/w/cpp/filesystem/copy_file

注意:copy_file(from, to, option) ,以下情况报错(crash):1)源文件不存在报错,2)目标文件不允许覆盖(option没设置覆盖属性)。

一些常用的方法:

#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>
using namespace std;
int main() {
  namespace fs = std::experimental::filesystem;
  cout << "current path: is " << fs::current_path() << endl;

  //to string
  string s = fs::current_path().generic_string();
  cout << s << endl;
  fs::path p1("c:\\tmp\\hello.txt");
  cout << "exists() = " << fs::exists(p1) << "\n"
       << "root_name() = " << p1.root_name() << "\n"
       << "root_path() = " << p1.root_path() << "\n"
       << "relative_path() = " << p1.relative_path() << "\n"
       << "parent_path() = " << p1.parent_path() << "\n"
       << "filename() = " << p1.filename() << "\n"
       << "stem() = " << p1.stem() << "\n"
       << "extension() = " << p1.extension() << "\n";

  auto p2 = p1.replace_extension(".bak");
  cout << "New filename: " << p2 << endl;

  auto p3 = p1.parent_path() / p1.stem();
  p3 += "_111.txt";
  cout << "New filename: " << p3 << endl;
  auto p = fs::current_path() / "1.txt";
  std::ofstream(p).put('a'); // 创建文件大小为 1
  std::cout << "File size = " << fs::file_size(p) << '\n';

  fs::remove(p);

  return 0;
}

/*输出
current path: is D:\git_clone\mycpp11\cpp11\test
D:/git_clone/mycpp11/cpp11/test
exists() = 0
root_name() = c:
root_path() = c:\
relative_path() = tmp\hello.txt
parent_path() = c:\tmp
filename() = hello.txt
stem() = hello
extension() = .txt
New filename: c:\tmp\hello.bak
New filename: c:\tmp\hello_111.txt
File size = 1
*/

 

### C++ 文件系统库的使用方法与实例 C++17引入了标准库中的`<filesystem>`头文件,这使得处理文件和目录变得更加直观和便捷。此部分功能极大地简化了路径操作、遍历目录以及查询文件属性等常见任务。 #### 基本概念 - **path 类型**: 表示文件或目录的位置。 - **directory_entry 类型**: 封装了一个 path 对象并提供了访问其状态的方法。 - **file_status 和 file_type 枚举类**: 描述文件的状态及其类型(如常规文件、目录等)。 #### 创建 Path 实例 可以利用字符串字面量来创建 `std::filesystem::path` 的对象: ```cpp #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { fs::path p("/usr/local/bin"); std::cout << "Path is: " << p.string() << '\n'; } ``` #### 获取当前工作目录 通过调用 `current_path()` 函数可以获得程序运行时所在的默认位置: ```cpp try { std::cout << "Current path is: " << fs::current_path().string() << "\n"; } catch (const fs::filesystem_error& e) { std::cerr << e.what() << '\n'; } ``` #### 遍历目录内容 为了读取指定文件夹下的所有条目,可采用迭代器方式循环输出每个子项的信息: ```cpp for (auto &entry : fs::directory_iterator(p)) { std::cout << entry.path().filename().string() << ": " << ((fs::is_directory(entry.status())) ? "Directory\n" : "File\n"); } ``` #### 判断是否存在某文件/目录 可以通过 `exists()` 方法快速判断目标是否存在于磁盘上: ```cpp if (fs::exists(p)) { std::cout << "The specified location exists.\n"; } else { std::cout << "No such file or directory found at this location.\n"; } ``` #### 复制、移动及删除文件 对于基本的操作如复制(`copy_file`)、重命名(`rename`)或是移除(`remove`)单个文件而言,这些函数都十分有用: ```cpp // Copy a single file from src to dst, overwriting if necessary. bool copied = fs::copy_file("source.txt", "destination.txt", fs::copy_options::overwrite_existing); // Rename 'oldname' into 'newname'. fs::rename("oldname.ext", "newname.ext"); // Remove all contents of the given directory recursively. fs::remove_all("temp_folder/"); ``` 以上就是关于如何在现代 C++ 中运用 `<filesystem>` 库的一些基础知识点[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值