Linux文件管理命令的C++实现(简单)

代码和思路

使用dirent.h库中的函数来模拟实现Linux中的文件管理功能,包括打开目录,列出文件与目录,删除文件与目录,新建目录。基本实现了无 -r -f等条件下的模拟。具体注释如下

#include<iostream>
#include <sys/stat.h>
#include<cstring>
#include"dirent.h"
#include <unistd.h>
#define FilePath "/home/michael/Desktop/linuxSimulation"
using namespace std;
​
//引导函数
void help() {
    cout << "------------------------------";
    cout << "操作如下:------------------------------" << endl;
    cout << "cd——进入某个文件夹目录" << endl;
    cout << "   cd / 进入根目录" << endl;
    cout << "   cd ..返回上级目录" << endl;
    cout << "   cd xxx(绝对路径或相对路径) 进入某个指定文件夹" << endl;
    cout << "ls——列出当前文件夹下的文件和文件夹" << endl;
    cout << "mkdir——在当前目录下新建文件夹" << endl;
    cout << "   mkdir xxx(绝对路径) 在某个绝对路径下创建文件夹" << endl;
    cout << "   mkdir xxx(相对路径) 在某个相对路径下创建文件夹" << endl;
    cout << "rm——删除文件" << endl;
    cout << "   rm xxx 删除当前目录下的某个文件" << endl;
    cout << "--------------------------------------------------------------------" << endl;
}
//打开根目录
int root() {
    if (!chdir(FilePath));//切换到根目录
    
    else {
        perror("error");//输出错误原因
        return -1;//不成功则直接返回
         }
}
//打开文件夹
void cd() {
    string cdCommand;
    cin >> cdCommand;
        if (cdCommand == "/") {
            if (!chdir(FilePath))
                cout << "已经成功切换到根目录" << endl;
            else perror("error");
        }
        else if (cdCommand == "..") {
            if(!chdir("../"))
            cout << "已经成功返回上级目录" << endl;
            else cout << "切换有误,请重新输入" << endl;
        }
        else {
            const char* cwd = cdCommand.data();//类型转换
            if (!chdir(cwd)) {
                cout << "已经成功跳转到该目录" << endl;
​
            }
            else perror("error");
        }
    
}
​
//列出当前文件下的文件和文件夹
int ls() {//要求,需要输出类型
    DIR* dir;
    struct  dirent* ptr;
    dir = opendir("./" );
    if (NULL == dir)
    {
        cout << "打开失败" << endl;
        return -1;
    }
    while ((ptr = readdir(dir)) != NULL)
    {
        printf("%s ",ptr->d_name);//指针返回文件名称并且输出
    }
    closedir(dir);
    cout << endl;
    return 0;
​
}
​
//在当前目录下新建文件夹
void mkdir1() {
    string dirname;
    cin >> dirname;
    const char* cwd = dirname.data();
​
    if (!mkdir(cwd,0777)) {//0777文件权限最高
        cout << "创建目录成功" << endl;
    }
    else perror("error");
}
​
//删除文件
void rm() {
    string dirname;
    cin >> dirname;
    const char* cwd = dirname.data();//类型转换
​
​
    if (!remove(cwd)) {
        cout << "delete successfully!" << endl;
    }
    else perror("error");//输出错误原因 
}
​
​
int main(int argc,char* argv[]) {
    help();
    root();
    while (1) {
        cout << "<" << getcwd(NULL, 0) << ">" ;
        string command;
        cin >> command;
        if (command == "cd") {
            cd();
        }
        else if (command == "ls") {
            ls();
        }
        else if (command == "mkdir") {
            mkdir1();
        }
        else if (command == "rm") {
            rm();
        }
        else cout << command << "既不是内部命令也不是外部命令,请重新输入" << endl;
        
    }
}

所学整理

  • 耦合度底的真正实现

以前都是在书本上看如何通过封装等等操作,这次终于有机会编写一个比较长的代码,通过函数调用从而减少main函数的体量,既容易修改,又美观。

  • #include<direct.h>等文件操作库

包括 chdir()mkdir()perror()const char* cwd = cdCommand.data();//类型转换remove()的函数调用以及用法,值得一提的是对于 chmod()改变权限函数的深入理解

改变权限函数

#include < sys/stat.h>

chmod()

如果成功则返回 TRUE,否则返回 FALSE。

语法 chmod(file,mode)

参数描述

file必需规定要检查的文件。

mode可选。规定新的权限。

mode 参数由 4 个数字组成:

第一个数字永远是 0,

第二个数字规定所有者的权限,

第二个数字规定所有者所属的用户组的权限,

第四个数字规定其他所有人的权限。

可能的值(如需设置多个权限,请对下面的数字进行总计):

// 所有者可读写,其他人没有任何权限
chmod("test.txt",0600);
​
// 所有者可读写,其他人可读
chmod("test.txt",0644);
​
// 所有者有所有权限,其他所有人可读和执行
chmod("test.txt",0755);
​
// 所有者有所有权限,所有者所在的组可读
chmod("test.txt",0740);

猜你喜欢

转载自blog.csdn.net/m0_61427031/article/details/132325800
今日推荐