收藏 分销(赏)

实验二--文件系统模拟设计.doc

上传人:精**** 文档编号:2646192 上传时间:2024-06-03 格式:DOC 页数:15 大小:150.04KB 下载积分:8 金币
下载 相关 举报
实验二--文件系统模拟设计.doc_第1页
第1页 / 共15页
实验二--文件系统模拟设计.doc_第2页
第2页 / 共15页


点击查看更多>>
资源描述
实验二 文件系统模拟设计 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 15 个人收集整理 勿做商业用途 实验二 文件系统模拟设计 一、实验目的 通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。 二、实验内容 为linux系统设计一个简单的二级文件系统。要求做到以下几点: (1)可以实现下列几条命令(至少4条); login 用户登录 dir 列文件目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 (2)列目录时要列出文件名、物理地址、保护码和文件长度; (3)源文件可以进行读写保 三、实验内容指导提示 (1)首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。 (2)用户创建的文件,可以编号存储于磁盘上.如file0,file1,file2.。.并以编号作为物理地址,在目录中进行登记。 ★★★★★《程序设计思想参考》 〈程序设计> (1)设计思想 本系统是模拟实现多用户多目录的文件系统(8个用户),在系统出现登录后 ,输入用户与口令,在用户登录系统后,可建立文件卷,将用户输入的文件保存在指定的文件中.系统的命令与其命令的具体实现,此模拟系统共提供了上述命令,并根据命令的含义与要求,用C++编程来完成所有具体操作。该系统可以模拟完成用户的登陆和验证,列出文件和目录,新建目录,改变目录,创立和编写文件,删除文件和退出系统等功能。【注:在linux下用g++编译C++】 (2)主要数据结构 用户结构:账号与密码结构 typedef struct users {  char     name[8];  char     pwd[10]; }users; 本系统有8个默认的用户名,前面是用户名,后面为密码,用户登陆时只要输入正确便可进入系统,否则提示失败要求重新输入。 users usrarray[8] = {  "usr1”,"usr1",  ”usr2",”usr2”,  "usr3”,”usr3”,  ”usr4”,”usr4",  ”usr5”,”usr5",  "usr6”,"usr6”,  "usr7",”usr7",  "usr8”,"usr8", }; (3)数据结构说明 a)文件结构链表 struct fnode {     char filename[FILENAME_LENGTH];  int  isdir;  int isopen;  char content[255];  fnode *parent;  fnode *child;  fnode *prev;  fnode *next; }; b)函数介绍 fnode *initfile(char filename[],int isdir);//初始化文件或目录   void createroot();//建立系统根目录  int run();系统运行  int findpara(char *topara);对参数进行处理  bool chklogin(char *users, char *pwd);检查账号与口令  void help();命令列表  int mkdir();建立目录  int create();建立文件  int read();读取文件  int write();写入文件  int del();删除文件  int cd();切换目录  int dir();文件与目录列表 (4)各模块流程图        (5)、模拟文件系统参考程序清单 #include "stdio。h” #include ”iostream.h" #include ”string。h" #include ”iomanip.h” #define FILENAME_LENGTH 10 //文件名称长度 #define COMMAND_LENGTH 10  //命令行长度 #define PARA_LENGTH 30    //参数长度     //账号结构  typedef struct users {  char     name[8];  char     pwd[10]; }users;  //文件结构 struct fnode {     char filename[FILENAME_LENGTH];  int  isdir;  int isopen;  char content[255];  fnode *parent;  fnode *child;  fnode *prev;  fnode *next; }; //账号 users usrarray[8] = {  ”usr1",”usr1”,  "usr2”,”usr2",  "usr3","usr3",  "usr4",”usr4”,  "usr5",”usr5”,  ”usr6”,”usr6”,  "usr7”,”usr7",  ”usr8","usr8”, };  fnode *initfile(char filename[],int isdir);  void createroot();  int run();  int findpara(char *topara);  bool chklogin(char *users, char *pwd);  void help();  int mkdir();  int create();  int read();  int write();  int del();  int cd();  int dir(); fnode *root,*recent,*temp,*ttemp; char para[PARA_LENGTH],command[COMMAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH]; //创建文件与目录结点  fnode* initfile(char filename[],int isdir) {     fnode *node=new fnode;     strcpy(node-〉filename,filename);     node—>isdir=isdir;  node—〉isopen=0;  node->parent=NULL;     node-〉child=NULL;  node-〉prev=NULL;  node—>next=NULL;  return node; } //创建文件存储结点 void createroot () {    recent=root=initfile(”/",1);    root—>parent=NULL;    root—〉child=NULL;    root—〉prev=root->next=NULL;    strcpy(para,”/”);      } int mkdir() {   temp=initfile(” ”,1);  cin>〉temp—〉filename;  if(recent-〉child==NULL)     {      temp—>parent=recent;   temp->child=NULL;   recent-〉child=temp;   temp—〉prev=temp—〉next=NULL;        }    else    {     ttemp=recent-〉child;  while(ttemp—〉next)  {     ttemp=ttemp—〉next;     if(strcmp(ttemp->filename,temp—>filename)==0&&ttemp->isdir==1)     {         printf(”对不起,目录已存在!”);      return 1;      }  }       ttemp->next=temp;    temp->parent=NULL;    temp—〉child=NULL;    temp-〉prev=ttemp;    temp—>next=NULL;      } return 1; } int create() {   temp=initfile(” ”,0);  cin>〉temp->filename;  cin>>temp—〉content;  if(recent->child==NULL)     {      temp->parent=recent;   temp—〉child=NULL;   recent—>child=temp;   temp—〉prev=temp->next=NULL;   cout〈〈”文件建立成功!”<<endl;   }    else    {     ttemp=recent—〉child;  while(ttemp->next)  {     ttemp=ttemp—>next;     if(strcmp(ttemp-〉filename,temp->filename)==0&&ttemp->isdir==0)     {         printf(”对不起,文件已存在!”);      return 1;      }  }       ttemp—〉next=temp;    temp—〉parent=NULL;    temp->child=NULL;    temp->prev=ttemp;    temp—>next=NULL;    cout〈〈”文件建立成功!”〈<endl;    }     return 1; } int dir() { int i=0,j=0; temp=new fnode; temp=recent; if(temp!=root) {cout〈〈"      <DIR>                         ”<〈"。.”〈〈endl;i++;} if(temp—>child==NULL) {     cout〈〈"Total: ”<〈” directors                  ” 〈<i<<"          files                ”〈〈 j 〈〈endl;  return 1; } temp=temp-〉child; while(temp) {     if(temp—>isdir)  {cout<〈”      <DIR〉                        "<<temp—>filename<<endl;i++;}  else  {cout<〈"      <FILE>                       ”〈<temp-〉filename<<endl;j++;}  temp=temp->next;  } cout<〈"Total: ”〈〈" directors                  ” 〈<i〈〈”          files                "<< j <〈endl; } int read() { char filename[FILENAME_LENGTH]; cin>>filename;    if(recent->child==NULL)    {    cout<〈"文件不存在!"〈<endl;    return 1;   }    if(strcmp(recent—>child-〉filename,filename)==0)    {     cout〈〈recent—>child-〉content〈〈endl;     return 1;   }    else    {    temp=recent—〉child;    while(temp—>next)    {    if(strcmp(temp->next-〉filename,filename)==0)    {cout〈<temp->next—〉content<〈endl;    return 1;}   }    cout<<”文件不存在!”<〈endl;       }   } int write() { char filename[FILENAME_LENGTH]; cin〉>filename;    if(recent—〉child==NULL)    {    cout<<”文件不存在!”<<endl;    return 1;   }    if(strcmp(recent-〉child->filename,filename)==0)    {     recent—〉child—>isopen=1;//设置文件标记为打开     cin>>recent-〉child-〉content;     recent-〉child—〉isopen=0;//设置文件标记为关闭     cout<<”文件写入成功!”〈〈endl;     return 1;   }    else    {    temp=recent—〉child;    while(temp->next)    {    if(strcmp(temp->next—>filename,filename)==0)    {     recent—〉child-〉isopen=1;//设置文件标记为打开        cin〉>temp->next->content;     recent—〉child-〉isopen=0;//设置文件标记为关闭     cout<<”文件写入成功!"〈〈endl;    return 1;}   }    cout〈〈”文件不存在!"〈<endl;       } } int cd() {  char topara[PARA_LENGTH];  cin〉〉topara;    if(strcmp(topara,"。。”)==0)    {       int i;    while(recent-〉prev)    recent=recent—>prev;    if(recent—〉parent)    {    recent=recent—〉parent;    }          i=strlen(para);    while(para[i]!='/’ && i>0) i-—;    if(i!=0)     para[i]=’\0’;    else     para[i+1]=’\0’; } else {  findpara(topara); } return 1; } int findpara(char *topara) {    int i=0;    int sign=1;    if(strcmp(topara,”/”)==0)    {     recent=root;     strcpy(para,”/”);     return 1;    }    temp=recent;    strcpy(temppara,para);    if(topara[0]==’/’)    {     recent=root—〉child;     i++;     strcpy(para,”/”);    }    else    {       if(recent!=NULL && recent!=root)           strcat(para,"/");         if(recent && recent—>child)   {    if(recent-〉isdir)            recent=recent—>child;          else    {       printf(”路径错误!\n”);    return 1;   }   } }    while(i<=strlen(topara) && recent)    {     int j=0;     if(topara[i]==’/' && recent-〉child)     {     i++;     if(recent->isdir)         recent=recent->child;     else     {printf(”路径错误\n”);         return 0;    }     strcat(para,”/");   }    while(topara[i]!=’/’ && i〈=strlen(topara))    {       recentpara[j]=topara[i];    i++;j++;    }    recentpara[j]='\0';    while((strcmp(recent—>filename,recentpara)!=0 || (recent-〉isdir!=1)) && recent—〉next!=NULL)    {        recent=recent->next;   }    if(strcmp(recent-〉filename,recentpara)==0)    {     if(recent—>isdir==0)     {strcpy(para,temppara);     recent=temp;     printf("是文件不是目录。\n");     return 0;    }     strcat(para,recent-〉filename);   }    if(strcmp(recent->filename,recentpara)!=0 || recent==NULL)    {    strcpy(para,temppara);    recent=temp;    printf("输入路径错误\n");    return 0;    }   } return 1; } int del() {   char filename[FILENAME_LENGTH];   cin〉>filename;     temp=new fnode;       if(recent-〉child)   {     temp=recent-〉child;  while(temp->next && (strcmp(temp->filename,filename)!=0 || temp—〉isdir!=0))         temp=temp->next;  if(strcmp(temp->filename,filename)!=0)  {     cout〈〈”不存在该文件!”〈〈endl;     return 0;  }  }   else   {     cout<〈"不存在该文件!"<〈endl;   return 0;  }     if(temp-〉parent==NULL)   {    temp—〉prev—〉next=temp—〉next;    if(temp->next)      temp—>next—>prev=temp-〉prev;         temp—>prev=temp—〉next=NULL;  }   else   {    if(temp—>next)     temp—〉next->parent=temp->parent;    temp-〉parent-〉child=temp-〉next;  }   delete temp;   cout<〈”文件已删除!"〈〈endl; }   bool chklogin(char *users, char *pwd) {  int i;  for(i=0; i<8; i++)  {   if( (strcmp(users,usrarray[i].name)==0) && (strcmp(pwd,usrarray[i].pwd)==0))    return true;  }  return false; } void help(void) {  cout<〈"                  命  令  一  览               ”<〈endl;  cout〈〈endl;  cout〈〈"create:             建立文件。                ”〈<endl;  cout〈〈"read:               读取文件。                  "〈<endl;  cout〈<"write:              写入文件,支持多线程          ”〈〈endl;  cout<〈”del   :             删除文件。                  ”<<endl;  cout<〈"mkdir:              建立目录。                ”〈〈endl;  cout〈〈"cd:                 切换目录。                  ”<〈endl;  cout<〈"logout:             退出登录。                ”〈〈endl; } int run() {   cout<<”linux:”<〈para〈〈"〉";   cin>>command;    if(strcmp(command,”mkdir”)==0)    mkdir();  else if(strcmp(command,"dir")==0)    dir();  else if(strcmp(command,"cd")==0)    cd();   else if(strcmp(command,"create”)==0)    create();  else if(strcmp(command,”read")==0)    read();  else if(strcmp(command,"write")==0)    write(); else if(strcmp(command,”del")==0)    del(); else if(strcmp(command,"help”)==0)    help();   else if(strcmp(command,”logout”)==0)  return 0;     else   cout<〈"请参考help提供的命令列表!"<〈endl;  } int main() { int i=0; bool in=false; char users[8],pwd[12];    cout〈〈”|———-——---——---—-—————---——-—---—---—————---—-—-————-----—---——-—-|”<<endl;    cout〈〈”|                       c语言模拟Linux文件系统                     |”<<endl;    cout〈<"|            账号:usr1-usr8      密码:usr1-usr8           |"〈<endl;    cout〈<”|                      你只有三次机会来试验账号                   |”〈<endl;    cout<〈”|                  键入help可以获取帮助                     |"<〈endl;    cout〈〈"|_________________________________________________________________|"<〈endl;    cout<〈endl; while(i〈3) { cout〈〈"Login:”; cin>>users; cout<<”Pass:”; cin〉>pwd; if(chklogin(users,pwd)) {in=true;break;} i++; } createroot(); while(in) { if(!run()) break; } } 六、实验实习或教学实习报告要求 (按照标准的实验报告要求书写) 七、实验实习成绩评定方式 根据学生的学习态度、上机完成结果等按“A、B、C、D”综合考核评定或按百分制评定。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服