C++17 filesystem备忘录

本文详细介绍了C++17中的文件系统库,包括如何在VS2019中启用C++17支持,讲解了path、directory_entry、directory_iterator等核心类以及常用库函数如exists、copy、remove等的使用方法,并提供了代码示例进行说明。

本文主要介绍C++17中的filesystem中的一些常用方法

一、C++17的支持

VS2019修改C++标准(支持C++17)_vs使用c++17_Italink的博客-优快云博客

二、头文件及命名空间

#include<filesystem>
using namespace std::filesystem;

三、常用类:
    path 类:说白了该类只是对字符串(路径)进行一些处理,这也是文件系统的基石。

    directory_entry 类:功如其名,文件入口,这个类才真正接触文件。 

    directory_iterator 类:获取文件系统目录中文件的迭代器容器,其元素为 directory_entry对象(可用于遍历目录)

    file_status 类:用于获取和修改文件(或目录)的属性(需要了解C++11的强枚举类型(即枚举类))

四、使用方法
    1. 需要有一个path对象为基础,如果需要修改路径,可以调用其成员函数进行修改(注意其实只是处理字符串)。

     2.需要获取文件信息需要通过path构造directory_entry,但需要path一定存在才能调用构造,所以需要实现调用exists(path .)函数确保目录存在才能构造directory_entry(注意文件入口中的exists无法判断)。

     3.若需遍历,则可以使用 directory_iterator,进行遍历

   演示如下:

#include <iostream>
#include<filesystem>
using namespace std;
using namespace std::filesystem;
int main(){
	path str("C:\\Windows");
	if (!exists(str))		//必须先检测目录是否存在才能使用文件入口.
		return 1;
	directory_entry entry(str);		//文件入口
	if (entry.status().type() == file_type::directory)	//这里用了C++11的强枚举类型
		cout << "该路径是一个目录" << endl;
	directory_iterator list(str);	        //文件入口容器
	for (auto& it:list) 
		cout << it.path().filename()<< endl;	//通过文件入口(it)获取path对象,再得到path对象的文件名,将之输出
	system("pause");
	return 0;
}

五、常用库函数
void copy(const path& from, const path& to) :目录复制

path absolute(const path& pval, const path& base = current_path()) :获取相对于base的绝对路径

bool create_directory(const path& pval) :当目录不存在时创建目录

bool create_directories(const path& pval) :形如/a/b/c这样的,如果都不存在,创建目录结构

bool exists(const path& pval) :用于判断path是否存在

uintmax_t file_size(const path& pval) :返回目录的大小

file_time_type last_write_time(const path& pval) :返回目录最后修改日期的file_time_type对象

bool remove(const path& pval) :删除目录

uintmax_t remove_all(const path& pval) :递归删除目录下所有文件,返回被成功删除的文件个数

void rename(const path& from, const path& to) :移动文件或者重命名

 

更多API信息,请参考:std::filesystem

找出下列代码的问题 #include <bits/stdc++.h> #include <windows.h> #include <ctime> #include <filesystem> using namespace std; typedef long long ll; typedef string str; typedef __int128 int128; typedef double dou; str dir_path = "D:\\user"; ifstream fin; ofstream fout; vector<str> todo; // 清屏函数 void clear() { system("cls"); } // 等待函数 void wait(dou second) { Sleep(second * 1000); } // 暂停函数 void stop() { system("pause"); } // 快速幂运算函数(支持负指数) dou qp(dou a, ll b) { if (b == 0) return 1.0; if (b < 0) { // 处理负指数 a = 1.0 / a; b = -b; } dou res = 1.0; while (b > 0) { if (b % 2 == 1) res *= a; a *= a; b /= 2; } return res; } // 输入输出模板函数 template<typename T, typename TT> T ioput(TT oput) { T iput; cout << oput; cin >> iput; return iput; } // 简单计算函数 template<typename T> dou calculate(T num1, char op, T num2) { switch (op) { case '+': return num1 + num2; case '-': return num1 - num2; case '*': return num1 * num2; case '/': if (num2 == 0) { cout << "错误:除数不能为零!" << endl; return 0; } return num1 / num2; case '^': return qp(num1, num2); // 使用改进的快速幂函数 default: cout << "错误:无效的运算符!" << endl; return 0; } } // 开始界面 void begin() { system("color 09"); if (!filesystem::exists(dir_path)) { // 检查目录是否存在 filesystem::create_directory(dir_path); // 创建目录 } filesystem::current_path(dir_path); // 切换工作目录 cout << "欢迎使用便携备忘录0.0.0" << endl; wait(0.5); } // 登录界面 str login() { str user; system(((str)"cd "+dir_path).c_str()); while (true) { clear(); user = ioput<str>("请输入用户ID:"); str file_name = user + "_password.txt"; fin.open(file_name, ios::in); // 只读模式 if (!fin) { // 如果文件不存在 fin.close(); fout.open(file_name, ios::out); // 写入模式 str password = ioput<str>("输入密码来创建账户:"); fout << password; fout.close(); clear(); cout << "账户创建成功!" << endl; break; } else { // 文件已存在 str password; fin >> password; fin.close(); str temp_password = ioput<str>("请输入密码登录:"); clear(); if (temp_password == password) { cout << "登录成功!" << endl; break; } else { cout << "密码错误,请重新登录" << endl; wait(0.5); } } } return user; } // 主功能菜单 void start() { while (true) { clear(); str s = ioput<str>("放入任务队列(1)\n进行简单计算(2)\n查看任务列表(3)\n退出(4)\n请选择: "); if (s == "1" || s == "放入任务队列") { str task = ioput<str>("请输入任务: "); todo.push_back(task); cout << "任务已添加!" << endl; stop(); } else if (s == "2" || s == "进行简单计算") { dou num1 = ioput<dou>("请输入第一个数字: "); char op = ioput<char>("请输入运算符 (+, -, *, /, ^): "); dou num2 = ioput<dou>("请输入第二个数字: "); dou result = calculate(num1, op, num2); if (result != 0) { // 如果没有发生错误,则显示结果 cout << "计算结果: " << result << endl; } stop(); } else if (s == "3" || s == "查看任务列表") { if (todo.empty()) { cout << "当前没有任务!" << endl; } else { cout << "当前任务列表:" << endl; for (size_t i = 0; i < todo.size(); ++i) { cout << i + 1 << ". " << todo[i] << endl; } } stop(); } else if (s == "4" || s == "退出") { clear(); cout << "感谢使用便携备忘录,再见!" << endl; stop(); break; } else { cout << "未知的指令,请重新输入" << endl; stop(); } } } // 保存函数 void save(const str& user_name) { clear(); system("cd "+dir_path); str file_name = user_name + "_tasks.txt"; fout.open(file_name, ios::out); for (const auto& task : todo) { fout << task << endl; } fout.close(); cout << "任务已保存" << endl; wait(0.1); clear(); } // 主函数 int main() { begin(); str user_name = login(); start(); save(user_name); // 保存任务列表 return 0; }
05-28
检查错误并补全代码: #include <bits/stdc++.h> #include <windows.h> #include <ctime> #include <filesystem> using namespace std; typedef long long ll; typedef string str; typedef __int128 int128; typedef double dou; typedef long double ldou; str dir_path = “D:\user”; ifstream fin; ofstream fout; vector<str> todo; // 清屏函数 void clear() { #ifdef _WIN32 system(“cls”); #else system(“clear”); #endif } // 等待函数 void wait(dou second) { Sleep(second * 1000); } // 暂停函数 void stop() { cout << “Press any key to continue…”; cin.ignore(); cin.get(); } // 快速幂运算函数(支持负指数) ldou qp(ldou a, ll b) { if (b == 0) return 1.0L; if (b < 0) { // 处理负指数 a = 1.0L / a; b = -b; } ldou res = 1.0L; while (b > 0) { if (b % 2 == 1) res *= a; a *= a; b /= 2; } return res; } // 输入输出模板函数 template<typename T> T ioput(const string& prompt) { T input; cout << prompt; cin >> input; return input; } // 简单计算函数 template<typename T> dou calculate(T num1, char op, T num2) { switch (op) { case ‘+’: return num1 + num2; case ‘-’: return num1 - num2; case ‘*’: return num1 * num2; case ‘/’: if (num2 == 0) { cout << “错误:除数不能为零!” << endl; return 0; } return num1 / num2; case ‘^’: return qp(num1, num2); // 使用改进的快速幂函数 default: cout << “错误:无效的运算符!” << endl; return 0; } } // 开始界面 void begin() { system((“color 09”).c_str()); if (!filesystem::exists(dir_path)) { // 检查目录是否存在 filesystem::create_directory(dir_path); // 创建目录 } filesystem::current_path(dir_path); // 切换工作目录 cout << “欢迎使用便携备忘录0.0.0” << endl; wait(0.5); } // 加载任务函数 void load_tasks(const str& user_name) { str file_name = user_name + “_tasks.txt”; if (filesystem::exists(file_name)) { ifstream fin(file_name.c_str()); // 使用 .c_str() 转换为 const char* str line; while (getline(fin, line)) { todo.push_back(line); } fin.close(); } } // 登录界面 str login() { str user; system(("cd " + dir_path).c_str()); // 使用 .c_str() 转换为 const char* while (true) { clear(); user = ioput<str>(“请输入用户ID:”); str file_name = user + “_password.txt”; fin.open(file_name.c_str(), ios::in); // 使用 .c_str() 转换为 const char* if (!fin) { // 如果文件不存在 fin.close(); fout.open(file_name.c_str(), ios::out); // 使用 .c_str() 转换为 const char* str password = ioput<str>(“输入密码来创建账户:”); fout << password; fout.close(); clear(); cout << “账户创建成功!” << endl; break; } else { // 文件已存在 str password; fin >> password; fin.close(); str temp_password = ioput<str>(“请输入密码登录:”); clear(); if (temp_password == password) { cout << “登录成功!” << endl; load_tasks(user); // 加载任务 break; } else { cout << “密码错误,请重新登录” << endl; wait(0.5); } } } return user; } //输出任务列表 void output() { if(todo.size()==0) { cout<<“当前没有任务\n”; return; } else { for(int i=0;i<todo.size();i++) { cout<<i+1<<“、”<<todo[i]<<endl; } } } // 主功能菜单 void start() { while (true) { clear(); str s = ioput<str>("放入任务队列(1)\n进行简单计算(2)\n退出(3)\n请选择: "); clear(); while (s == “1” || s == “管理任务列表”) { clear(); str choose = ioput<str>("1、添加任务\n2、删除任务\n3、任务优先级排序\n4、退出\n请输入选项: "); clear(); if(choose==“1”||choose==“添加任务”) { str task=ioput<str>(“请输入任务:\n”); clear(); todo.push_back(task); cout<<“任务添加成功\n”; stop(); } else if(choose==“2”||choose==“删除任务”) { int number=ioput<int>(“请输入要删除的任务编号”); clear(); str do_=ioput<str>(“确定删除吗:Y/N”); if(do_==“N”) { continue; } todo.erase(todo+number-1); cout<<“删除成功\n”; stop(); } else if(choose==“3”||choose==“任务优先级排序”) { cout<<“请安要求输入编号:\n”; str first=ioput<str>(“操作任务:”); str second=ioput<str>(“被操作任务:”); int tool=ioput<int>(“操作方式\n:1-移动到被操作任务前\n2-交换\n”); clear(); if(tool==1) { } else if() { } else { cout<< } cout<<"操作成功"; stop(); } else if(choose=="4"||choose=="退出") { } clear(); } if (s == "2" || s == "进行简单计算") { dou num1 = ioput<dou>("请输入第一个数字: "); char op = ioput<char>("请输入运算符 (+, -, *, /, ^): "); dou num2 = ioput<dou>("请输入第二个数字: "); dou result = calculate(num1, op, num2); if (result != 0) { // 如果没有发生错误,则显示结果 cout << "计算结果: " << result << endl; } stop(); } else if (s == "3" || s == "退出") { clear(); cout << "感谢使用便携备忘录,再见!" << endl; stop(); break; } else { cout << "未知的指令,请重新输入" << endl; stop(); } } } // 保存函数 void save(const str& user_name) { clear(); system(("cd " + dir_path).c_str()); // 使用 .c_str() 转换为 const char* str file_name = user_name + “_tasks.txt”; fout.open(file_name.c_str(), ios::out); // 使用 .c_str() 转换为 const char* for (const auto& task : todo) { fout << task << endl; } fout.close(); cout << “任务已保存” << endl; wait(0.1); clear(); } // 主函数 int main() { begin(); str user_name = login(); start(); save(user_name); // 保存任务列表 return 0; }
05-27
为什么下面会提示_chdir和_mkdir两个函数没有被定义 #include <bits/stdc++.h> #include <windows.h> #include <ctime> using namespace std; typedef long long ll; typedef string str; typedef __int128 int128; typedef double dou; ifstream fin; ofstream fout; vector<string> todo; // 修正:使用标准的 vector<string> // 清屏函数 void clear() { system(“cls”); } // 等待函数 void wait(dou second) { Sleep(second * 1000); } // 暂停函数 void stop() { system(“pause”); } // 快速幂运算函数(修正:支持负指数) dou qp(dou a, ll b) { if (b == 0) return 1.0; if (b < 0) { // 处理负指数 a = 1.0 / a; b = -b; } dou res = 1.0; while (b > 0) { if (b % 2 == 1) res *= a; a *= a; b /= 2; } return res; } // 输入输出模板函数 template<typename T, typename TT> T ioput(TT oput) { T iput; cout << oput; cin >> iput; return iput; } // 简单计算函数(修正:避免类型转换问题) template<typename T> dou calculate(T num1, char op, T num2) { switch (op) { case ‘+’: return num1 + num2; case ‘-’: return num1 - num2; case ‘*’: return num1 * num2; case ‘/’: if (num2 == 0) { cout << “错误:除数不能为零!” << endl; return 0; } return num1 / num2; case ‘^’: return qp(num1, num2); // 使用改进的快速幂函数 default: cout << “错误:无效的运算符!” << endl; return 0; } } // 开始界面 void begin() { system(“color 09”); if (_chdir(“D:\user_password”) != 0) { // 修正:使用 _chdir 更改工作目录 _mkdir(“D:\user_password”); // 创建目录 } cout << “欢迎使用便携备忘录0.0.0” << endl; wait(1.5); } // 登录界面(修正:避免文件冲突) str login() { str user; while (true) { clear(); user = ioput<str>(“请输入用户ID:”); str file_name = user + “_password.txt”; fin.open(file_name, ios::in); // 只读模式 if (!fin) { // 如果文件不存在 fin.close(); fout.open(file_name, ios::out); // 写入模式 str password = ioput<str>(“输入密码来创建账户:”); fout << password; fout.close(); cout << “账户创建成功!” << endl; break; } else { // 文件已存在 str password; fin >> password; fin.close(); str temp_password = ioput<str>(“请输入密码登录:”); if (temp_password == password) { cout << “登录成功!” << endl; break; } else { cout << “密码错误,请重新登录” << endl; wait(0.5); } } } return user; } // 主功能菜单 void start() { while (true) { clear(); str s = ioput<str>("放入任务队列(1)\n进行简单计算(2)\n查看任务列表(3)\n退出(4)\n请选择: "); if (s == “1” || s == “放入任务队列”) { str task = ioput<str>("请输入任务: "); todo.push_back(task); cout << “任务已添加!” << endl; stop(); } else if (s == “2” || s == “进行简单计算”) { dou num1 = ioput<dou>("请输入第一个数字: "); char op = ioput<char>("请输入运算符 (+, -, *, /, ^): "); dou num2 = ioput<dou>("请输入第二个数字: "); dou result = calculate(num1, op, num2); if (result != 0) { // 如果没有发生错误,则显示结果 cout << "计算结果: " << result << endl; } stop(); } else if (s == "3" || s == "查看任务列表") { if (todo.empty()) { cout << "当前没有任务!" << endl; } else { cout << "当前任务列表:" << endl; for (size_t i = 0; i < todo.size(); ++i) { cout << i + 1 << ". " << todo[i] << endl; } } stop(); } else if (s == "4" || s == "退出") { cout << "感谢使用便携备忘录,再见!" << endl; stop(); break; } else { cout << "未知的指令,请重新输入" << endl; stop(); } } } // 保存函数(实现:保存任务列表到文件) void save(const str& user_name) { str file_name = user_name + “_tasks.txt”; fout.open(file_name, ios::out); for (const auto& task : todo) { fout << task << endl; } fout.close(); cout << “任务已保存到文件:” << file_name << endl; } // 主函数 int main() { begin(); str user_name = login(); start(); save(user_name); // 保存任务列表 return 0; }
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值