文件管理程序C++

时间: 2023-07-11 13:17:08 浏览: 49
好的,你可以尝试以下的文件管理程序C++代码: ```c++ #include <iostream> #include <fstream> #include <string> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> using namespace std; void list_files(string path) { DIR *dir; struct dirent *ent; if ((dir = opendir(path.c_str())) != NULL) { while ((ent = readdir(dir)) != NULL) { if (ent->d_name[0] != '.') { cout << ent->d_name << endl; } } closedir(dir); } else { cerr << "Could not open directory: " << path << endl; } } void create_file(string path) { ofstream file(path.c_str()); if (file.is_open()) { cout << "File created successfully!" << endl; } else { cerr << "Could not create file: " << path << endl; } file.close(); } void delete_file(string path) { if (remove(path.c_str()) == 0) { cout << "File deleted successfully!" << endl; } else { cerr << "Could not delete file: " << path << endl; } } void rename_file(string old_path, string new_path) { if (rename(old_path.c_str(), new_path.c_str()) == 0) { cout << "File renamed successfully!" << endl; } else { cerr << "Could not rename file: " << old_path << endl; } } void copy_file(string old_path, string new_path) { ifstream src(old_path.c_str(), ios::binary); ofstream dst(new_path.c_str(), ios::binary); dst << src.rdbuf(); if (src.is_open() && dst.is_open()) { cout << "File copied successfully!" << endl; } else { cerr << "Could not copy file: " << old_path << " to " << new_path << endl; } src.close(); dst.close(); } void move_file(string old_path, string new_path) { if (rename(old_path.c_str(), new_path.c_str()) == 0) { cout << "File moved successfully!" << endl; } else { cerr << "Could not move file: " << old_path << " to " << new_path << endl; } } void get_file_info(string path) { struct stat file_info; if (stat(path.c_str(), &file_info) == 0) { cout << "File name: " << path << endl; cout << "File size: " << file_info.st_size << " bytes" << endl; cout << "File permissions: "; cout << ((file_info.st_mode & S_IRUSR) ? "r" : "-") << ((file_info.st_mode & S_IWUSR) ? "w" : "-") << ((file_info.st_mode & S_IXUSR) ? "x" : "-"); cout << ((file_info.st_mode & S_IRGRP) ? "r" : "-") << ((file_info.st_mode & S_IWGRP) ? "w" : "-") << ((file_info.st_mode & S_IXGRP) ? "x" : "-"); cout << ((file_info.st_mode & S_IROTH) ? "r" : "-") << ((file_info.st_mode & S_IWOTH) ? "w" : "-") << ((file_info.st_mode & S_IXOTH) ? "x" : "-"); cout << endl; } else { cerr << "Could not get file info: " << path << endl; } } int main() { string path; int choice; do { cout << "Please enter the path of the directory: "; getline(cin, path); if (path[path.length() - 1] != '/') { path += '/'; } cout << "1. List files" << endl; cout << "2. Create file" << endl; cout << "3. Delete file" << endl; cout << "4. Rename file" << endl; cout << "5. Copy file" << endl; cout << "6. Move file" << endl; cout << "7. Get file info" << endl; cout << "0. Exit" << endl; cout << "Please enter your choice: "; cin >> choice; cin.ignore(); switch (choice) { case 1: list_files(path); break; case 2: cout << "Please enter the name of the file to create: "; getline(cin, path); create_file(path); break; case 3: cout << "Please enter the name of the file to delete: "; getline(cin, path); delete_file(path); break; case 4: string old_path, new_path; cout << "Please enter the name of the file to rename: "; getline(cin, old_path); cout << "Please enter the new name of the file: "; getline(cin, new_path); rename_file(old_path, new_path); break; case 5: cout << "Please enter the name of the file to copy: "; getline(cin, old_path); cout << "Please enter the name of the new file: "; getline(cin, new_path); copy_file(old_path, new_path); break; case 6: cout << "Please enter the name of the file to move: "; getline(cin, old_path); cout << "Please enter the new name of the file: "; getline(cin, new_path); move_file(old_path, new_path); break; case 7: cout << "Please enter the name of the file to get info: "; getline(cin, path); get_file_info(path); break; case 0: break; default: cerr << "Invalid choice!" << endl; break; } } while (choice != 0); return 0; } ``` 这个文件管理程序可以列出指定目录下的所有文件、创建文件、删除文件、重命名文件、复制文件、移动文件和获取文件信息。你可以根据需要进行修改和扩展。

相关推荐

最新推荐

recommend-type

学籍管理系统源代码 c++.docx

为了学校更加高效,快捷,方便的管理学生信息,并实现以下功能: (1)对学生信息进行录入:先输入学生的学籍,然后输入学生姓名,年龄,性别,籍贯,系别,专业,班级等,最后输入学生状态(入学)。 (2)对已录取...
recommend-type

汽车租赁信息管理系统源代码 c++.docx

一、为了方便公司管理各种型号的车辆,并实现以下功能: (1)对车辆进行租赁:先输入车牌号,然后输入车辆类别、品牌型号,并在库存中查找该车辆的相关信息,并进行租车。 (2)添加新的车辆信息:主要完成车辆信息...
recommend-type

C++实现学生学习成绩管理程序

该系统实现对若干个大学生的学习成绩进行管理。至少包括以下信息: 学号、姓名、科目、成绩,学期。学期取值范围可为1-8。 功能要求: 1.使用中文菜单; 2. 将学生信息保存在文本文档中,具体对学生信息进行插入...
recommend-type

C++图书馆管理程序.docx(带有完整源代码,类的声明和定义分开,非常清晰)

完整版的C++图书馆管理程序,包括对问题的分析,类的设计与声明,还画了类的层次图;运用了继承,和多态,使用了vector,以及文件读取;使用了人员继承和书本继承。
recommend-type

c++程序设计基础课程设计学生成绩管理系统实验报告

储存与读写处理(即是说,总按具有连续编号num为“序”来对文件中的各对象数据进行随机读写处理)。并设计该类的成员函数,而且对输出运算符“”进行重载,使该运算符能够完成将一个学生的信息输出到屏幕上。要求...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。