刚学C++时老师布置的一个实验作业。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
using namespace std;
vector<string> split_str(string s);
class fileOperate
{
public:
int md(string name){
mode_t mode = 0775;
if(mkdir(name.c_str(), mode)==0)
{
cout<<"创建文件夹‘"<<name<<"’成功"<<endl;
return 0;
}
else
{
cout<<"error: 创建文件夹‘"<<name<<"’失败。"<<endl;
return -1;
}
}
int del(string fileName){
if(remove(fileName.c_str()) == 0 )
{
cout<<"删除文件‘"<<fileName<<"’成功"<<endl;
return 0;
}
else
{
cout<<"error: 删除文件‘"<<fileName<<"’失败。"<<endl;
return -1;
}
}
int cd(string path){
return chdir(path.c_str())==0? 0 : -1;
}
void ls(){
//列出文件夹内容
DIR * curDir;
struct dirent *ent;
struct stat entStat;
if((curDir=opendir("."))==NULL)
{
cout<<"error: 打开当前文件夹失败。"<<endl;
}
else
{
while((ent=readdir(curDir))!=NULL)//遍历该文件目录下的内容
{
if(stat(ent->d_name, &entStat) == 0)
{
if(entStat.st_mode & S_IFDIR)//判断是否是文件夹
{
cout<<setw(6)<<left<<"d";
cout<<right<<setw(10)<<entStat.st_size<<left<<setw(8)<<" bytes";
cout<<left<<setw(8)<<entStat.st_mode;
cout<<left<<setw(12)<<entStat.st_atime;
cout<<ent->d_name<<"/"<<endl;
}
else if(entStat.st_mode & S_IFREG)
{
cout<<setw(6)<<left<<"-";
cout<<right<<setw(10)<<entStat.st_size<<left<<setw(8)<<" bytes";
cout<<left<<setw(8)<<entStat.st_mode;
cout<<left<<setw(12)<<entStat.st_atime;
cout<<ent->d_name<<endl;
}
}
}
closedir(curDir);
}
}
int cp(string oldfile,string oldfilePath,string newfilePath){//深度拷贝
struct dirent *ent;
struct stat entStat,entStat2;
cd(oldfilePath);
if( stat(oldfile.c_str(),&entStat)==0){
if(entStat.st_mode & S_IFDIR){
cd(newfilePath);
if(md(oldfile)==0){
DIR * curDir;
cd(oldfilePath);
cd(oldfile);
curDir=opendir(".");
while((ent=readdir(curDir))!=NULL)
{
if(stat(ent->d_name, &entStat2) == 0)
{
if(ent->d_name[0]!='.'){//跳过本身和复文件
string newpath=newfilePath+'/'+oldfile;
string oldpath=oldfilePath+'/'+oldfile;
cp(ent->d_name,oldpath,newpath);
cd(oldfilePath);
cd(oldfile);
}
}
}
closedir(curDir);
cd("..");
}else
return -1;
}else{
cd(oldfilePath);
ifstream file(oldfile.c_str(),ios::binary);
if(!file){
cout<<"打开文件失败,无法复制"<<endl;
return -1;
}
cd(newfilePath);
ofstream file2(oldfile.c_str(),ios::binary);
if(!file2){
cout<<"不能创建新文件,复制失败"<<endl;
return -1;
}
file2<<file.rdbuf();
file.close();
file2.close();
return 0;
}
}
return 0;
}
};
vector<string> split_str(string s)//处理输入指令
{
vector<string> tokens;
istringstream iss(s);//以空格为分割符号,把该行分割开
do
{
string sub;
iss >> sub;
tokens.push_back(sub);
} while (iss);
tokens.erase(tokens.end()-1); // 删除最后的换行回车符
return tokens;
}
int main()
{
fileOperate operate;
cout<<"=============================================="<<endl;
cout<<"命令格式: ? xx1 xx2 …… xxN"<<endl;
cout<<" md 创建文件夹 del 删除文件夹/文件夹"<<endl;
cout<<" cd 改变当前文件夹 ls 列出文件夹内容"<<endl;
cout<<" cp 复制文件/文件夹 q 退出\n"<<endl;
cout<<"请输入";
string commandLine;
vector<string> commandArgs;
getline(cin,commandLine);
commandArgs=split_str(commandLine);
while(commandArgs.size()>0 && "q"!=commandArgs[0])//按回车或者输入q都能退出程序
{
if(commandArgs[0]=="cd"){
if(commandArgs.size()<2)
cout<<"请输入文件夹名字"<<endl;
else{
if(operate.cd(commandArgs[1])==0){
cout<<"切换成功"<<endl;
}else
cout<<"切换失败"<<endl;
}
}else if(commandArgs[0]=="md"){
if(commandArgs.size()<2)
cout<<"请输入文件夹名字"<<endl;
else
operate.md(commandArgs[1]);
}else if(commandArgs[0]=="ls"){
operate.ls();
}else if(commandArgs[0]=="del"){
if(commandArgs.size()<2){
cout<<"请输入文件夹名称"<<endl;
}else{
cout<<commandArgs[1]<<endl;
if(operate.del(commandArgs[1])==0){
cout<<"删除成功"<<endl;
}else{
cout<<"删除文件夹失败"<<endl;
}
}
}else if(commandArgs[0]=="cp"){
if(commandArgs.size()!=4){
cout<<"请以这个格式输入/“ cp 源文件名称 源文件路径 目标文件夹名称/”"<<endl;
}else{
if(operate.cp(commandArgs[1],commandArgs[2],commandArgs[3])==0)
cout<<"复制成功"<<endl;
else
cout<<"复制失败"<<endl;
}
}
cout<<"?";//执行玩任务,继续让用户输入指令
getline(cin,commandLine);
commandArgs=split_str(commandLine);
}
return 0;
}