由于涉及到文件操作,需要使用系统调用函数,因此需要引入头文件<sys/types.h>、<sys/stat.h>、<fcntl.h>、<unistd.h>

#include #include #include #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>

using namespace std;

const int MAX_PATH_LEN = 256; char current_dir[MAX_PATH_LEN]; // 存储当前目录路径

void print_prompt() { cout << "myfs@" << current_dir << "$ "; }

void ls() { // 打开当前目录 DIR *dir = opendir(current_dir); if (dir == NULL) { cout << "open directory failed" << endl; return; }

// 读取目录下的文件和文件夹
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
    cout << entry->d_name << " ";
}
cout << endl;

// 关闭目录
closedir(dir);

}

void cd(string path) { // 如果是绝对路径,直接更改当前目录 if (path[0] == '/') { strcpy(current_dir, path.c_str()); return; }

// 如果是相对路径,需要加上当前目录路径再更改
string new_path = string(current_dir) + "/" + path;
strcpy(current_dir, new_path.c_str());

}

void mv(string from_path, string to_path) { // 打开源文件 int fd_from = open(from_path.c_str(), O_RDONLY); if (fd_from == -1) { cout << "open source file failed" << endl; return; }

// 创建目标文件
int fd_to = open(to_path.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd_to == -1) {
    cout << "create target file failed" << endl;
    close(fd_from);
    return;
}

// 读取源文件内容并写入目标文件
char buf[1024];
int n;
while ((n = read(fd_from, buf, sizeof(buf))) > 0) {
    write(fd_to, buf, n);
}

// 关闭文件
close(fd_from);
close(fd_to);

// 删除源文件
remove(from_path.c_str());

}

void touch(string path) { // 创建文件 int fd = open(path.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (fd == -1) { cout << "create file failed" << endl; return; }

// 关闭文件
close(fd);

}

void mkdir(string path) { // 创建目录 int ret = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); if (ret == -1) { cout << "create directory failed" << endl; } }

void rm(string path) { // 删除文件 int ret = remove(path.c_str()); if (ret == -1) { cout << "remove file failed" << endl; } }

void rmdir(string path) { // 删除目录 int ret = rmdir(path.c_str()); if (ret == -1) { cout << "remove directory failed" << endl; } }

void read(string path) { // 打开文件 int fd = open(path.c_str(), O_RDONLY); if (fd == -1) { cout << "open file failed" << endl; return; }

// 读取文件内容并输出
char buf[1024];
int n;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
    cout.write(buf, n);
}

// 关闭文件
close(fd);

}

void write(string path, string content) { // 打开文件 int fd = open(path.c_str(), O_WRONLY | O_TRUNC); if (fd == -1) { cout << "open file failed" << endl; return; }

// 写入内容
write(fd, content.c_str(), content.length());

// 关闭文件
close(fd);

}

int main() { // 初始化当前目录为根目录 strcpy(current_dir, "/");

string cmd;
while (true) {
    print_prompt();
    getline(cin, cmd);

    // 解析命令和参数
    string::size_type pos = cmd.find(' ');
    string command;
    string arg1, arg2;
    if (pos == string::npos) {
        command = cmd;
    } else {
        command = cmd.substr(0, pos);
        string::size_type pos2 = cmd.find(' ', pos + 1);
        if (pos2 == string::npos) {
            arg1 = cmd.substr(pos + 1);
        } else {
            arg1 = cmd.substr(pos + 1, pos2 - pos - 1);
            arg2 = cmd.substr(pos2 + 1);
        }
    }

    // 执行命令
    if (command == "ls") {
        ls();
    } else if (command == "cd") {
        cd(arg1);
    } else if (command == "mv") {
        mv(arg1, arg2);
    } else if (command == "touch") {
        touch(arg1);
    } else if (command == "mkdir") {
        mkdir(arg1);
    } else if (command == "rm") {
        rm(arg1);
    } else if (command == "rmdir") {
        rmdir(arg1);
    } else if (command == "read") {
        read(arg1);
    } else if (command == "write") {
        write(arg1, arg2);
    } else if (command == "exit") {
        break;
    } else {
        cout << "unknown command" << endl;
    }
}

return 0;
1ls查看当前目录下的文件和文件夹信息命令。2cd进入下级目录命令。3mv移动文件命令4touch:新建文件命令5mkdir:新建文件夹命令6rm删除文件命令7rmdir删除文件夹命令8read:从某文件内读取信息命令9write:向某文件内写入信息命令10exit:退出文件系统命令请实现一个具有以上功能的文件系统给出具体c++代码

原文地址: http://www.cveoy.top/t/topic/fQZK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录