【问题标题】:Directory Structure for a unix like file system类 Unix 文件系统的目录结构
【发布时间】:2016-11-29 04:34:32
【问题描述】:

我目前正在学习系统软件课程,我们的最终项目是实现一个简单的 unix 类 shell 环境和具有分层目录结构的文件系统。我们已经完成了向用户询问“cd xxx”或“ls”等命令的简单部分。一旦每个命令被调用,它就会进入一个函数。我知道我需要一个树状的目录和文件数据结构,但我只是不知道从哪里开始。我知道父级只能是一个目录。该目录有一个名称,可以采用其他目录和文件。一个文件只有一个名字。我该如何实现这种代码?这就是我现在所拥有的一切:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>


void makeFS(){
    printf("You created and formatted a new filesystem.\n");
}

void listDir(){
    printf("Listing all entries in current directory...\n");
}

void exitShell(){
    printf("Adios amigo.\n");
    exit(0);
}

void makeDir(char name[50]){

   printf("Directory [%s] created at !\n", name);
}

void remDir(char cmd[50]){
    printf("You entered %s \n", cmd);
}

void changeDir(char *nextName){
    printf("Changed directory path to %s\n", nextName);
}

void status(char cmd[50]){
    printf("You entered %s \n", cmd);
}

void makeFile(char cmd[50]){
    printf("You entered %s \n", cmd);
}

void remFile(char cmd[50]){
    printf("You entered %s \n", cmd);
}

int main(int argc, char *argv[]){

    char cmd[50];
    const char spc[50] = " \n";
    char *file, *dir;
    char *tok, *nextName;


    while (1){
        printf("Russ_John_Shell> ");
        fgets(cmd, 50, stdin);

        //Tokenizes string to determine what command was inputed as well as     any file/directory name needed
        tok = strtok(cmd, spc);
        nextName = strtok(NULL, spc);


        //Checks to see whether the string has a file/directory name after the command
        if (nextName == NULL){
            //mkfs command
            if (strcmp(cmd, "mkfs") == 0){
                makeFS();
            }
            //exit command
            else if (strcmp(cmd, "exit") == 0){
                exitShell();
            } 
            //ls command
            else if (strcmp(cmd, "ls") == 0){
                listDir();
            }
            //command not recognized at all
            else {
                printf("Command not recognized.\n");
            }
        }
        else {
            //mkdir command
            if (strcmp(cmd, "mkdir") == 0){
                makeDir(nextName);
            }
            //rmdir command
            else if (strcmp(cmd, "rmdir") == 0){
                remDir(cmd);
            }
            //cd command
            else if (strcmp(cmd, "cd") == 0){
                changeDir(nextName);
            }
            //stat command
            else if (strcmp(cmd, "stat") == 0){
                status(cmd);
            }
            //mkfile command
            else if (strcmp(cmd, "mkfile") == 0){
                makeFile(cmd);
            }    
            //rmfile command
            else if (strcmp(cmd, "rmfile") == 0){
                remFile(cmd);
            }
            //command not recognized at all
            else {
                printf("Command not recognized.\n");
            }
        }
    }
}

【问题讨论】:

  • 首先,摆脱所有那些cmd[50] 声明。定义一个参数,比如#define CMDLEN 50
  • @paulsm4 或者更好的是,使用std::string,因为它被标记为 C++。

标签: c++ unix filesystems directory-structure


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 2011-09-08
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多