操作系统模拟实验——文件系统(创建、打开、关闭、读取、写入、删除文件,创建、删除和列目录)

一、实验目的

  1. 理解文件系统的主要组成和工作原理。

  2. 学习并掌握文件的存储方式,文件的创建、读写、删除等操作的实现机制。

  3. 提高对文件系统内部组织和管理的认识及熟悉实际系统使用上的细节。

二、实验要求

  1. 设计并实现一个可以支持树状目录的文件系统。

  2. 能够执行基本的文件操作,如创建、打开、关闭、读取、写入、删除文件,创建、删除和列目录等。

  3. 通过终端接口与用户交互,实现对模拟文件系统的操作。

  4. 不能使用与文件操作相关的系统调用来完成该模拟文件系统的设计,需要基于自定义的文件、目录等数据结构来实现对文件、目录的管理。

三、实验代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>

using namespace std;

const int FCB_NUM = 20;//文件控制块数量
const int BLOCK_NUM = 100;//外存块数量
const string ROOTPATH = "D:";//根路径

//文件类型
enum FileType{
    DIRECTORY,//目录文件
    TXT,//txt文件
};

//文件存取权限
enum FileAuthority{
    READ_ONLY,//只读
    READ_WRITE,//读写
};

//定义文件控制块
struct FCB{
    string name;//文件名
    FileType type;//文件类型
    FileAuthority authority;//存取权限
    int blockNum;//物理位置,存放的外存块号
}f[FCB_NUM];

int fcbCnt = 0;//当前已有的FCB个数

//定义外存,表示一个外存块
struct Block{
    int value = 0;//外存块中是否存放数据,0代表外存块为空,1代表外存块存在文件,初始状态为0
    int fcbNum = -1;//存放的文件对应的fcb块号,-1代表外存块未存放文件
    string txtData = "";//如果是txt文本文件,存放字符串,初始为空
    vector<int> directory;//如果是目录文件,存放fcb下标序列
}block[BLOCK_NUM];

//根目录
int rootDirectoryPos;

//当前路径
int currentDirectory;//当前目录的外存块号
string currentPath;

//FAT文件分配表,fat[i]代表i号外存块对应的下一个外存块的下标
int fat[BLOCK_NUM];

//空闲盘块链
struct FreeBlock{
    int id = -1;//空闲块号
    FreeBlock* next = NULL;//该空闲块指向的下一个空闲块
};

//空闲盘块链链头及链尾
FreeBlock* freeBlockHead;
FreeBlock* freeBlockTail;

//获得一个空闲块
int GetFreeBlock()
{
    FreeBlock* b = freeBlockHead;
    int id = b->id;

    FreeBlock* tmp = b;
    freeBlockHead = b->next;
    free(tmp);//释放内存空间
    return id;
}

//释放一个外存块,并将其放在空闲盘块连链的链尾
void RealiseBlock(int id)
{
    FreeBlock* tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
    tmp->id = id;
    tmp->next = NULL;
    freeBlockTail->next = tmp;
    freeBlockTail = tmp;
}

//打印当前空闲盘块链
void PrintBlock()
{
    FreeBlock* b = freeBlockHead;
    printf("当前空闲盘块链:\n");
    while(b != NULL)
    {
        printf("%d->", b->id);
        b = b->next;
    }
    printf("NULL\n");
}

//文件系统的初始化
void InitialFileSystem()
{
    //初始化外存块,设定20个空闲块,其余80个外存块存放其他数据
    int freeBlockList[20] = {1,8,14,19,21,27,33,37,44,45,57,59,61,62,75,77,81,85,93,99};
    for(int i = 0; i < BLOCK_NUM; i++)
        block[i].value = 1;
    for(int i = 0; i < 20; i++)
        block[freeBlockList[i]].value = 0;

    //初始化FAT
    for(int i = 0; i < BLOCK_NUM; i++)
        fat[i] = -1;//初始状态fat[i]均为-1

    //初始化空闲链表,采用尾插法
    FreeBlock *tmp;
    tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
    tmp->id = freeBlockList[0];
    tmp->next = NULL;
    freeBlockHead = tmp;
    freeBlockTail = tmp;

    for(int i = 1; i < 20; i++){
        tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
        tmp->id = freeBlockList[i];
        tmp->next = NULL;
        if(freeBlockHead->next == NULL)
            freeBlockHead->next = tmp;
        freeBlockTail->next = tmp;
        freeBlockTail = tmp;
    }
    //初始化根目录
    int bid = GetFreeBlock();//获得一个空闲块的下标
    rootDirectoryPos = bid;
    currentDirectory = bid;//将当前目录设置为根目录
    currentPath = ROOTPATH;
    block[bid].value = 1;
}

//展示当前目录(文件夹)
void ShowCurrentDiretory()
{
    printf("-------------------------------------\n");//分界线
    printf("当前目录:%s\n",currentPath.c_str());
    if(block[currentDirectory].directory.size() == 0)
    {
        printf("当前目录未存放任何文件和目录,请新建文件!\n");
        return;
    }

    printf("%8s %8s %8s %8s\n", "文件名  ", "文件类型", "存取权限", "物理位置");
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        string type = (f[tmp_id].type == TXT) ? "TXT文件" : "目录";
        string authority = (f[tmp_id].authority == READ_ONLY) ? "只读" : "读写";
        printf("%-8s %-8s %-8s %-8d\n", f[tmp_id].name.c_str(), type.c_str(), authority.c_str(), f[tmp_id].blockNum);
    }
    printf("-------------------------------------\n");//分界线
}

//创建文件
void CreateFile()
{
    //设置文件类型
    printf("请选择您要创建的文件类型:\n");
    printf("1.txt文本文件\n");
    printf("2.目录(文件夹)\n");
    int typeOp;
    cin >> typeOp;
    if(typeOp == 1)//创建txt文本文件
        f[++fcbCnt].type = TXT;
    else//创建目录(文件夹)
        f[++fcbCnt].type = DIRECTORY;

    //设置文件物理位置
    int bid = GetFreeBlock();//获得一个空闲块的下标
    f[fcbCnt].blockNum = bid;

    //修改当前目录文件
    block[currentDirectory].directory.push_back(fcbCnt);

    //设置文件名称
    while(1)
    {
        printf("请输入该文件的名称:\n");
        string name;
        cin >> name;
        //检查是否存在文件重名情况
        bool flag = 0;
        for(int i = 0; i < block[currentDirectory].directory.size(); i++)
        {
            int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
            if(f[tmp_id].name == name)
            {
                flag = 1;
                break;
            }
        }
        if(flag)//如果存在文件重名情况
        {
            printf("当前目录已存在名称为%s的文件,请重新输入!\n", name.c_str());
            continue;
        }
        f[fcbCnt].name = name;
        break;
    }

    //设置文件内容
    block[f[fcbCnt].blockNum].value = 1;
    if(f[fcbCnt].type == TXT)
    {
        printf("请输入文件内容(一段字符串):\n");
        string txtData;
        getchar();
        getline(cin, txtData);
        block[f[fcbCnt].blockNum].txtData = txtData;
    }

    //设置文件存取权限
    if(f[fcbCnt].type == TXT)
    {
        printf("请设置该文件的存取权限:\n");
        printf("1.只读\n");
        printf("2.读写\n");
        int authority;
        cin >> authority;
        if(authority == 1)
            f[fcbCnt].authority = READ_ONLY;
        else
            f[fcbCnt].authority = READ_WRITE;
    }
    else
        f[fcbCnt].authority = READ_WRITE;
}

//打开文件
void OpenFile()
{
    printf("请输入你要读取的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要打开的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }

    if(f[fcb_id].type == TXT)//如果该文件是txt文本文件
    {
        printf("文件%s的内容如下所示:\n", f[fcb_id].name.c_str());
        printf("%s\n",block[f[fcb_id].blockNum].txtData.c_str());
    }
    else//如果该文件是目录文件
    {
        currentDirectory = f[fcb_id].blockNum;//更新当前目录
        currentPath = currentPath + "\\" + f[fcb_id].name;//更新当前路径
    }
}

//写入文件
void WriteFile()
{
    printf("请输入要写入的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要写入的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }

    if(f[fcb_id].type == TXT)//如果该文件是txt文本文件
    {
        if(f[fcb_id].authority == READ_ONLY)
        {
            printf("你不具备%s文件的写权限!\n", name.c_str());
            return;
        }
        else
        {
            printf("文件%s的内容如下所示:\n", f[fcb_id].name.c_str());
            printf("%s\n",block[f[fcb_id].blockNum].txtData.c_str());
            printf("请输入新的文件内容:\n");
            string newTxtData;
            getchar();
            getline(cin, newTxtData);
            block[f[fcb_id].blockNum].txtData = newTxtData;
        }
    }
    else//如果该文件是目录文件
    {
        printf("该文件是目录文件,只能写入txt文件,请重试!\n", name.c_str());
        return;
    }
}

//删除文件
void DeleteFile()
{
    printf("请输入要删除的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要删除的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }
    
    //修改当前目录文件
    vector<int>::iterator it;
    for(it = block[currentDirectory].directory.begin(); it != block[currentDirectory].directory.end(); it++)
    {
        if(*it == fcbCnt)
        {
            block[currentDirectory].directory.erase(it);
            break;
        }
    }
}

//返回上一级目录
void ReturnToPre()
{
    if(currentPath == ROOTPATH)
    {
        printf("当前目录已经是根目录,无法返回上一级!\n");
        return;
    }
    int xb = currentPath.rfind('\\');//从字符串末尾开始查找字符'\',记录下标
    currentPath = currentPath.substr(0, xb);
    if(currentPath == ROOTPATH)//如果上一级目录是根目录
    {
        currentDirectory = rootDirectoryPos;
    }
    else
    {
        xb = currentPath.rfind('\\');//再次从字符串末尾开始查找字符'\',记录下标
        string name = currentPath.substr(xb + 1);//截取上一级目录的名称
        for(int i = 0; i < FCB_NUM; i++)//在FCB中查找上一级目录
        {
            if(f[i].name == name)
            {
                currentDirectory = f[i].blockNum;
                break;
            }
        }
    }
    
}

//显示选择面板
void ShowPenel()
{
    printf("-------------------------------------\n");//分界线
    PrintBlock();//打印当前空闲盘块链

    ShowCurrentDiretory();//展示当前目录

    printf("请选择你接下来要执行的操作:\n");
    printf("1.新建文件\n");
    printf("2.打开文件\n");
    printf("3.删除文件\n");
    printf("4.写入文件\n");
    printf("5.返回上一级目录\n");
    printf("6.退出系统\n");
    printf("-------------------------------------\n");//分界线
}

//操作选择模块
void ChooseOption()
{
    int op;
    do
    {
        ShowPenel();
        cin >> op;
        switch(op)
        {
            //新建文件
            case 1:
                CreateFile();
                break;
            //打开文件
            case 2:
                OpenFile();
                break;
            //删除文件
            case 3:
                DeleteFile();
                break;
            //写入文件
            case 4:
                WriteFile();
                break;
            //返回上一级
            case 5:
                ReturnToPre();
                break;
            //退出系统
            default:
                break;
        }
    }while(op != 6);
}

int main()
{
    InitialFileSystem();//初始化文件系统
    ChooseOption();//选择将要执行的操作
    return 0;
}

四、实验代码解读

4.1前言

​ 模拟实验代码语言为c++。

4.2数据结构

4.2.1FCB的数据结构

//文件类型
enum FileType{
    DIRECTORY,//目录文件
    TXT,//txt文件
};

//文件存取权限
enum FileAuthority{
    READ_ONLY,//只读
    READ_WRITE,//读写
};
//定义文件控制块
struct FCB{
    string name;//文件名
    FileType type;//文件类型
    FileAuthority authority;//存取权限
    int blockNum;//物理位置,存放的外存块号
}f[FCB_NUM];

  设置FCB文件控制块用来保存文件相关信息,例如:文件名name、文件类型type、文件存取权限authority和文件存放的物理位置blockNum。

4.2.2外存的数据结构

//定义外存,表示一个外存块
struct Block{
    int value = 0;//外存块中是否存放数据,0代表外存块为空,1代表外存块存在文件,初始状态为0
    int fcbNum = -1;//存放的文件对应的fcb块号,-1代表外存块未存放文件
    string txtData = "";//如果是txt文本文件,存放字符串,初始为空
    vector<int> directory;//如果是目录文件,存放fcb下标序列
}block[BLOCK_NUM];

  Block表示外存,存放Value、文件对应的fcb块号fcbNum、文本文件数据txtData和目录文件数据directory。

4.2.3根目录和当前路径

//根目录
int rootDirectoryPos;

//当前路径
int currentDirectory;//当前目录的外存块号
string currentPath;

  rootDirectoryPos表示根目录所存放的外存块号,currentDirectory表示当前目录存放的外存块号,currentPath表示当前路径。

4.2.4空闲盘块链

//空闲盘块链
struct FreeBlock{
    int id = -1;//空闲块号
    FreeBlock* next = NULL;//该空闲块指向的下一个空闲块
};

//空闲盘块链链头及链尾
FreeBlock* freeBlockHead;
FreeBlock* freeBlockTail;

  该文件系统采用空闲盘块链来管理空闲块。FreeBlock里面保存空闲块号和下一个FreeBlock的指针。设置freeBlockHead和freeBlockTail表示空闲盘块链的链头和链尾。

4.3主要函数

4.3.1获得一个空闲块

//获得一个空闲块
int GetFreeBlock()
{
    FreeBlock* b = freeBlockHead;
    int id = b->id;

    FreeBlock* tmp = b;
    freeBlockHead = b->next;
    free(tmp);//释放内存空间
    return id;
}

  该函数从空闲盘块链的链头取一个空闲块。

4.3.2释放一个空闲块

//释放一个外存块,并将其放在空闲盘块链的链尾
void RealiseBlock(int id)
{
    FreeBlock* tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
    tmp->id = id;
    tmp->next = NULL;
    freeBlockTail->next = tmp;
    freeBlockTail = tmp;
}

  该函数释放一个外存块,并将其放在空闲盘块链的链尾。

4.3.3打印当前空闲盘块链

//打印当前空闲盘块链
void PrintBlock()
{
    FreeBlock* b = freeBlockHead;
    printf("当前空闲盘块链:\n");
    while(b != NULL)
    {
        printf("%d->", b->id);
        b = b->next;
    }
    printf("NULL\n");
}

  该函数打印出当前的空闲盘块链,向用户展示信息。

4.3.4文件系统的初始化

//文件系统的初始化
void InitialFileSystem()
{
    //初始化外存块,设定20个空闲块,其余80个外存块存放其他数据
    int freeBlockList[20] = {1,8,14,19,21,27,33,37,44,45,57,59,61,62,75,77,81,85,93,99};
    for(int i = 0; i < BLOCK_NUM; i++)
        block[i].value = 1;
    for(int i = 0; i < 20; i++)
        block[freeBlockList[i]].value = 0;

    //初始化空闲链表,采用尾插法
    FreeBlock *tmp;
    tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
    tmp->id = freeBlockList[0];
    tmp->next = NULL;
    freeBlockHead = tmp;
    freeBlockTail = tmp;

    for(int i = 1; i < 20; i++){
        tmp = (FreeBlock*)malloc(sizeof(FreeBlock));
        tmp->id = freeBlockList[i];
        tmp->next = NULL;
        if(freeBlockHead->next == NULL)
            freeBlockHead->next = tmp;
        freeBlockTail->next = tmp;
        freeBlockTail = tmp;
    }
    //初始化根目录
    int bid = GetFreeBlock();//获得一个空闲块的下标
    rootDirectoryPos = bid;
    currentDirectory = bid;//将当前目录设置为根目录
    currentPath = ROOTPATH;
    block[bid].value = 1;
}

  文件系统初始化函数主要进行这些操作:初始化外存块、初始化空闲链表和初始化根目录。

4.3.5创建文件

//创建文件
void CreateFile()
{
    //设置文件类型
    printf("请选择您要创建的文件类型:\n");
    printf("1.txt文本文件\n");
    printf("2.目录(文件夹)\n");
    int typeOp;
    cin >> typeOp;
    if(typeOp == 1)//创建txt文本文件
        f[++fcbCnt].type = TXT;
    else//创建目录(文件夹)
        f[++fcbCnt].type = DIRECTORY;

    //设置文件物理位置
    int bid = GetFreeBlock();//获得一个空闲块的下标
    f[fcbCnt].blockNum = bid;

    //修改当前目录文件
    block[currentDirectory].directory.push_back(fcbCnt);

    //设置文件名称
    while(1)
    {
        printf("请输入该文件的名称:\n");
        string name;
        cin >> name;
        //检查是否存在文件重名情况
        bool flag = 0;
        for(int i = 0; i < block[currentDirectory].directory.size(); i++)
        {
            int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
            if(f[tmp_id].name == name)
            {
                flag = 1;
                break;
            }
        }
        if(flag)//如果存在文件重名情况
        {
            printf("当前目录已存在名称为%s的文件,请重新输入!\n", name.c_str());
            continue;
        }
        f[fcbCnt].name = name;
        break;
    }

    //设置文件内容
    block[f[fcbCnt].blockNum].value = 1;
    if(f[fcbCnt].type == TXT)
    {
        printf("请输入文件内容(一段字符串):\n");
        string txtData;
        getchar();
        getline(cin, txtData);
        block[f[fcbCnt].blockNum].txtData = txtData;
    }

    //设置文件存取权限
    if(f[fcbCnt].type == TXT)
    {
        printf("请设置该文件的存取权限:\n");
        printf("1.只读\n");
        printf("2.读写\n");
        int authority;
        cin >> authority;
        if(authority == 1)
            f[fcbCnt].authority = READ_ONLY;
        else
            f[fcbCnt].authority = READ_WRITE;
    }
    else
        f[fcbCnt].authority = READ_WRITE;
}

  该函数创建一个文件。用户需要选择要创建的文件类型,选择文件的存取权限、设置文件名称和设置文件内容等。该函数会为该文件分配物理位置,并修改相应目录信息。

4.3.6删除文件

//删除文件
void DeleteFile()
{
    printf("请输入要删除的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要删除的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }
    
    //修改当前目录文件
    vector<int>::iterator it;
    for(it = block[currentDirectory].directory.begin(); it != block[currentDirectory].directory.end(); it++)
    {
        if(*it == fcbCnt)
        {
            block[currentDirectory].directory.erase(it);
            break;
        }
    }
}

  该函数根据用户输入的文件名称,在当前目录查找要删除的文件,删除并修改当前目录。

4.3.7打开文件

//打开文件
void OpenFile()
{
    printf("请输入你要读取的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要打开的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }

    if(f[fcb_id].type == TXT)//如果该文件是txt文本文件
    {
        printf("文件%s的内容如下所示:\n", f[fcb_id].name.c_str());
        printf("%s\n",block[f[fcb_id].blockNum].txtData.c_str());
    }
    else//如果该文件是目录文件
    {
        currentDirectory = f[fcb_id].blockNum;//更新当前目录
        currentPath = currentPath + "\\" + f[fcb_id].name;//更新当前路径
    }
}

  该函数根据用户输入的文件名称,查找要打开的文件。如果是txt文本文件,显示其内容;如果是目录文件,就进入下一级目录并更新当前路径。

4.3.8写入文件

//写入文件
void WriteFile()
{
    printf("请输入要写入的文件的名称:\n");
    string name;
    cin >> name;
    int fcb_id = -1;//获取要写入的文件的fcb下标,初始为-1
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        if(f[tmp_id].name == name)
            fcb_id = tmp_id;
    }

    if(fcb_id == -1)//如果匹配失败
    {
        printf("未在当前目录中找到名称为%s的文件,请重试!\n", name.c_str());
        return;
    }

    if(f[fcb_id].type == TXT)//如果该文件是txt文本文件
    {
        if(f[fcb_id].authority == READ_ONLY)
        {
            printf("你不具备%s文件的写权限!\n", name.c_str());
            return;
        }
        else
        {
            printf("文件%s的内容如下所示:\n", f[fcb_id].name.c_str());
            printf("%s\n",block[f[fcb_id].blockNum].txtData.c_str());
            printf("请输入新的文件内容:\n");
            string newTxtData;
            getchar();
            getline(cin, newTxtData);
            block[f[fcb_id].blockNum].txtData = newTxtData;
        }
    }
    else//如果该文件是目录文件
    {
        printf("该文件是目录文件,只能写入txt文件,请重试!\n", name.c_str());
        return;
    }
}

  该函数根据用户输入的文件名称,查找要写入的文件,并判断用户是否具有该文件的写权限,以此做出相应处理。

4.3.9返回上一级目录

//返回上一级目录
void ReturnToPre()
{
    if(currentPath == ROOTPATH)
    {
        printf("当前目录已经是根目录,无法返回上一级!\n");
        return;
    }
    int xb = currentPath.rfind('\\');//从字符串末尾开始查找字符'\',记录下标
    currentPath = currentPath.substr(0, xb);
    if(currentPath == ROOTPATH)//如果上一级目录是根目录
    {
        currentDirectory = rootDirectoryPos;
    }
    else
    {
        xb = currentPath.rfind('\\');//再次从字符串末尾开始查找字符'\',记录下标
        string name = currentPath.substr(xb + 1);//截取上一级目录的名称
        for(int i = 0; i < FCB_NUM; i++)//在FCB中查找上一级目录
        {
            if(f[i].name == name)
            {
                currentDirectory = f[i].blockNum;
                break;
            }
        }
    }
}

  该函数从当前路径中查找到上一级目录名称,然后根据上一级目录名称查找到上一级目录的FCB,最后达到返回上一级目录的效果。

4.4用户交互

4.4.1显示选择面板

//显示选择面板
void ShowPenel()
{
    printf("-------------------------------------\n");//分界线
    PrintBlock();//打印当前空闲盘块链

    ShowCurrentDiretory();//展示当前目录

    printf("请选择你接下来要执行的操作:\n");
    printf("1.新建文件\n");
    printf("2.打开文件\n");
    printf("3.删除文件\n");
    printf("4.写入文件\n");
    printf("5.返回上一级目录\n");
    printf("6.退出系统\n");
    printf("-------------------------------------\n");//分界线
}

  该函数向用户显示选择面板。

4.4.2操作选择模块

//操作选择模块
void ChooseOption()
{
    int op;
    do
    {
        ShowPenel();
        cin >> op;
        switch(op)
        {
            //新建文件
            case 1:
                CreateFile();
                break;
            //打开文件
            case 2:
                OpenFile();
                break;
            //删除文件
            case 3:
                DeleteFile();
                break;
            //写入文件
            case 4:
                WriteFile();
                break;
            //返回上一级
            case 5:
                ReturnToPre();
                break;
            //退出系统
            default:
                break;
        }
    }while(op != 6);
}

  操作选择模块根据用户选择的操作,执行相应的函数。

4.4.3展示当前目录

//展示当前目录(文件夹)
void ShowCurrentDiretory()
{
    printf("-------------------------------------\n");//分界线
    printf("当前目录:%s\n",currentPath.c_str());
    if(block[currentDirectory].directory.size() == 0)
    {
        printf("当前目录未存放任何文件和目录,请新建文件!\n");
        return;
    }

    printf("%8s %8s %8s %8s\n", "文件名  ", "文件类型", "存取权限", "物理位置");
    for(int i = 0; i < block[currentDirectory].directory.size(); i++)
    {
        int tmp_id = block[currentDirectory].directory[i];//获取当前目录下文件的fcb下标
        string type = (f[tmp_id].type == TXT) ? "TXT文件" : "目录";
        string authority = (f[tmp_id].authority == READ_ONLY) ? "只读" : "读写";
        printf("%-8s %-8s %-8s %-8d\n", f[tmp_id].name.c_str(), type.c_str(), authority.c_str(), f[tmp_id].blockNum);
    }
    printf("-------------------------------------\n");//分界线
}

  该函数向用户展现当前目录,提供文件名、文件类型、存取权限和物理位置等信息。

4.4.4主函数

int main()
{
    InitialFileSystem();//初始化文件系统
    ChooseOption();//选择将要执行的操作
    return 0;
}

  主函数依次执行初始化文件系统模块和操作选择模块。

五、实验结果

在这里插入图片描述

  由于截图数量过多,这里就只放一张图了。(最后一个实验有点偷懒,这里的交互部分写的挺烂的,容易看花眼,只能说勉强能用吧qwq)

  测试用例1:创建txt文件text。内容是This is a text!

1
1
text
This is a text!
2

  测试用例2:folder1在根目录下,folder1文件夹下有子文件夹folder2和txt文件text,删除folder2文件,修改text文件内容为This is a new text!

1
2
folder1
2
folder1

1
1
text
This is a text!
2

1
2
folder2
2
folder2

5
3
folder2

4
text
This is a new text!

2
text

  测试用例3:与测试数据2相似,仅将text文件的权限修改为只读。

1
2
folder1
2
folder1

1
1
text
This is a text!
1

1
2
folder2
2
folder2

5
3
folder2

4
text

六、结语

  本实验设计思路参考王道考研操作系统,90%以上代码为本人原创。如果有什么错误,欢迎大伙儿在评论区指正ヾ(●´▽‘●)ノ。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值