简单文件系统的实现

  1. #include <stdio.h>  
  2. #include <memory.h>  
  3. #include <string>  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. //1代表普通文件2代表目录文件0表示空文件  
  8. #define GENERAL 1  
  9. #define DIRECTORY 2  
  10. #define NULL 0  
  11.   
  12. struct FCB  
  13. {  
  14.     char fname[16]; //文件名  
  15.     char type;      
  16.     int size;    //文件大小  
  17.     int fatherBlockNum;    //当前的父目录盘块号  
  18.     int currentBlockNum;    //当前的盘块  
  19.   
  20.     void initialize()  
  21.     {  
  22.         strcpy(fname,"/0");  
  23.         type = NULL;  
  24.         size =0;  
  25.         fatherBlockNum = currentBlockNum = 0;  
  26.     }  
  27. };   
  28.   
  29. /*常量设置*/  
  30. const char* FilePath = "C://myfiles";  
  31. const int BlockSize = 512;       //盘块大小  
  32. const int OPEN_MAX = 5;          //能打开最多的文件数  
  33. const int BlockCount = 128;   //盘块数  
  34. const int DiskSize = BlockSize*BlockCount;    //磁盘大小  
  35. const int BlockFcbCount = BlockSize/sizeof(FCB);//目录文件的最多FCB数  
  36.   
  37. int OpenFileCount = 0;  
  38.   
  39. struct OPENLIST      //用户文件打开表  
  40. {  
  41.     int files;      //当前打开文件数  
  42.     FCB f[OPEN_MAX];    //FCB拷贝  
  43.     OPENLIST()  
  44.     {  
  45.         files=0;  
  46.         for(int i=0;i<OPEN_MAX;i++){  
  47.             f[i].fatherBlockNum=-1;//为分配打开  
  48.             f[i].type=GENERAL;  
  49.         }  
  50.     }  
  51. };  
  52.   
  53. /*-------------目录文件结构---------------*/  
  54. struct dirFile  
  55. {  
  56.     struct FCB fcb[BlockFcbCount];  
  57.     void init(int _FatherBlockNum,int _CurrentBlockNum,char *name)//父块号,当前块号,目录名  
  58.     {  
  59.         strcpy(fcb[0].fname,name); //本身的FCB  
  60.         fcb[0].fatherBlockNum=_FatherBlockNum;  
  61.         fcb[0].currentBlockNum=_CurrentBlockNum;  
  62.         fcb[0].type=DIRECTORY;     //标记目录文件  
  63.         for(int i=1;i<BlockFcbCount;i++){  
  64.             fcb[i].fatherBlockNum=_CurrentBlockNum; //标记为子项  
  65.             fcb[i].type=NULL;    // 标记为空白项  
  66.         }  
  67.     }  
  68. };  
  69.   
  70. /**********************************************************************/  
  71. struct DISK  
  72. {  
  73.     int FAT1[BlockCount];     //FAT1  
  74.     int FAT2[BlockCount];     //FAT2  
  75.     struct dirFile root;    //根目录  
  76.     char data[BlockCount-3][BlockSize];  
  77.     void format()  
  78.     {  
  79.         memset(FAT1,0,BlockCount);     //FAT1  
  80.         memset(FAT2,0,BlockCount);     //FAT2  
  81.         FAT1[0]=FAT1[1]=FAT1[2]=-2; //0,1,2盘块号依次代表FAT1,FAT2,根目录区  
  82.         FAT2[0]=FAT2[1]=FAT2[2]=-2;  //FAT作备份  
  83.         root.init(2,2,"C://");//根目录区  
  84.         memset(data,0,sizeof(data));//数据区  
  85.     }  
  86. };  
  87.   
  88.   
  89. /*-----------------全局变量--------------------------*/  
  90. FILE *fp;      //磁盘文件地址  
  91. char * BaseAddr;    //虚拟磁盘空间基地址  
  92. string currentPath="C://";   //当前路径  
  93. int current=2;    //当前目录的盘块号  
  94. string cmd;      //输入指令  
  95. struct DISK *osPoint;    //磁盘操作系统指针  
  96. char command[16];    //文件名标识  
  97. struct OPENLIST* openlist; //用户文件列表指针  
  98.   
  99. /*-----------函数事先申明--------------------*/  
  100. int  format();  
  101. int  mkdir(char *sonfname);  
  102. int rmdir(char *sonfname);  
  103. int create(char *name);  
  104. int listshow();  
  105. int delfile(char *name);  
  106. int changePath(char *sonfname);  
  107. int write(char *name);  
  108. int exit();  
  109. int open(char *file);  
  110. int close(char *file);  
  111. int  read(char *file);  
  112. /*------------初始化-----------------------*/  
  113. int format()  
  114. {  
  115.     current = 2;  
  116.     currentPath="C://";   //当前路径  
  117.     osPoint->format();//打开文件列表初始化  
  118.     delete openlist;  
  119.     openlist=new OPENLIST;  
  120.     /*-------保存到磁盘上myfiles--------*/  
  121.     fp = fopen(FilePath,"w+");  
  122.     fwrite(BaseAddr,sizeof(char),DiskSize,fp);  
  123.     fclose(fp);  
  124.     printf("----------------------------------------------------------/n/n");  
  125.     return 1;  
  126. }  
  127.   
  128. /*-----------------------创建子目录-------------------*/  
  129. int  mkdir(char *sonfname)  
  130. {  
  131.     //判断是否有重名  
  132.     //寻找空白子目录项  
  133.     //寻找空白盘块号  
  134.     //当前目录下增加该子目录项  
  135.     //分配子目录盘块,并且初始化  
  136.     //修改fat表  
  137.     int i,temp,iFAT;  
  138.     struct dirFile *dir;     //当前目录的指针  
  139.     if(current==2)  
  140.         dir=&(osPoint->root);  
  141.     else  
  142.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  143.     /*--------为了避免该目录下同名文件夹--------*/  
  144.     for(i = 1;i<BlockFcbCount;i++)  
  145.     {  
  146.         if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0 )  
  147.         {  
  148.             printf("该文件夹下已经有同名的文件夹存在了!/n");  
  149.             return 0;  
  150.         }  
  151.     }  
  152.     //查找空白fcb序号  
  153.     for(i=1;i<BlockFcbCount;i++)  
  154.     {  
  155.         if(dir->fcb[i].type==NULL)  
  156.             break;  
  157.     }  
  158.   
  159.     if(i==BlockFcbCount)  
  160.     {  
  161.         printf("该目录已满!请选择新的目录下创建!/n");  
  162.         return 0;  
  163.     }  
  164.   
  165.     temp=i;  
  166.   
  167.     for(i = 3;i < BlockCount;i++)  
  168.     {  
  169.         if(osPoint->FAT1[i] == 0)  
  170.             break;  
  171.     }  
  172.   
  173.     if(i == BlockCount)  
  174.     {  
  175.         printf("磁盘已满!/n");  
  176.         return 0;  
  177.     }  
  178.   
  179.     iFAT=i;  
  180.   
  181.     /*-------------接下来进行分配----------*/  
  182.   
  183.     osPoint->FAT1[iFAT]=osPoint->FAT2[iFAT] = 2;   //2表示分配给下级目录文件  
  184.     //填写该分派新的盘块的参数  
  185.     strcpy(dir->fcb[temp].fname,sonfname);  
  186.     dir->fcb[temp].type=DIRECTORY;  
  187.     dir->fcb[temp].fatherBlockNum=current;  
  188.     dir->fcb[temp].currentBlockNum=iFAT;  
  189.     //初始化子目录文件盘块  
  190.     dir=(struct dirFile*)(osPoint->data [iFAT-3]);   //定位到子目录盘块号  
  191.     dir->init (current,iFAT,sonfname);//iFAT是要分配的块号,这里的current用来指要分配的块的父块号  
  192.     printf("---------------------------------------------------------------/n/n");  
  193.     return 1;  
  194. }  
  195.   
  196.   
  197. /*-------删除当前目录下的文件夹--------*/  
  198. int rmdir(char *sonfname)  
  199. {  
  200.   
  201.     int i,temp,j;//确保当前目录下有该文件,并记录下该FCB下标  
  202.     struct dirFile *dir;     //当前目录的指针  
  203.     if(current==2)  
  204.         dir=&(osPoint->root);  
  205.     else  
  206.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  207.   
  208.     for(i=1;i<BlockFcbCount;i++)  
  209.     {     //查找该目录文件  
  210.         if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0)  
  211.         {  
  212.             break;  
  213.         }  
  214.     }  
  215.   
  216.     temp=i;  
  217.   
  218.     if(i==BlockFcbCount)  
  219.     {  
  220.         printf("当前目录下不存在该子目录!/n");  
  221.         return 0;  
  222.     }  
  223.   
  224.     j = dir->fcb[temp].currentBlockNum;  
  225.     struct dirFile *sonDir;     //当前子目录的指针  
  226.     sonDir=(struct dirFile *)(osPoint->data [ j - 3]);  
  227.   
  228.     for(i=1;i<BlockFcbCount;i++)  //查找子目录是否为空目录  
  229.     {  
  230.         if(sonDir->fcb[i].type!=NULL)  
  231.         {  
  232.             printf("该文件夹为非空文件夹,为确保安全,请清空后再删除!/n");  
  233.             return 0;  
  234.         }  
  235.     }  
  236.     /*开始删除子目录操作*/  
  237.     osPoint->FAT1[j] = osPoint->FAT2[j]=0;     //fat清空  
  238.     char *p=osPoint->data[j-3];      //格式化子目录  
  239.     memset(p,0,BlockSize);  
  240.     dir->fcb[temp].initialize();          //回收子目录占据目录项  
  241.     printf("---------------------------------------------------------------/n/n");  
  242.     return 1;  
  243. }  
  244.   
  245. /*-----------在当前目录下创建文本文件---------------*/  
  246. int create(char *name)  
  247. {  
  248.     int i,iFAT;//temp,  
  249.     int emptyNum = 0,isFound = 0;        //空闲目录项个数  
  250.     struct dirFile *dir;     //当前目录的指针  
  251.     if(current==2)  
  252.         dir=&(osPoint->root);  
  253.     else  
  254.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  255.   
  256.     //查看目录是否已满  
  257.     //为了避免同名的文本文件  
  258.     for(i=1;i<BlockFcbCount;i++)  
  259.     {  
  260.         if(dir->fcb[i].type == NULL && isFound == 0)  
  261.         {  
  262.             emptyNum = i;  
  263.             isFound = 1;  
  264.         }  
  265.         else if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,name)==0 )  
  266.         {  
  267.             printf("无法在同一目录下创建同名文件!/n");  
  268.             return 0;  
  269.         }  
  270.     }  
  271.   
  272.     if(emptyNum == 0)  
  273.     {  
  274.         printf("已经达到目录项容纳上限,无法创建新目录!/n");  
  275.         return 0;  
  276.     }  
  277.   
  278.     //查找FAT表寻找空白区,用来分配磁盘块号j  
  279.     for(i = 3;i<BlockCount;i++)  
  280.     {  
  281.         if(osPoint->FAT1[i]==0)  
  282.             break;  
  283.     }  
  284.     if(i==BlockCount)  
  285.     {  
  286.         printf("磁盘已满!/n");  
  287.         return 0;  
  288.     }  
  289.     iFAT=i;  
  290.   
  291.     /*------进入分配阶段---------*/  
  292.     //分配磁盘块  
  293.     osPoint->FAT1[iFAT] = osPoint->FAT2[iFAT] = 1;  
  294.     /*-----------接下来进行分配----------*/  
  295.   
  296.     //填写该分派新的盘块的参数  
  297.     strcpy(dir->fcb[emptyNum].fname,name);  
  298.     dir->fcb[emptyNum].type=GENERAL;  
  299.     dir->fcb[emptyNum].fatherBlockNum=current;  
  300.     dir->fcb[emptyNum].currentBlockNum=iFAT;  
  301.     dir->fcb[emptyNum].size =0;  
  302.     char* p = osPoint->data[iFAT -3];  
  303.     memset(p,4,BlockSize);  
  304.     printf("----------------------------------------------------------------/n/n");  
  305.     return 1;  
  306. }  
  307.   
  308. /*-------查询子目录------------*/  
  309. int listshow()  
  310. {  
  311.     int i,DirCount=0,FileCount=0;  
  312.     //搜索当前目录  
  313.     struct dirFile *dir;     //当前目录的指针  
  314.     if(current==2)  
  315.         dir=&(osPoint->root);  
  316.     else  
  317.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  318.   
  319.     for(i=1;i<BlockFcbCount;i++)  
  320.     {  
  321.         if(dir->fcb[i].type==GENERAL)  
  322.         {   //查找普通文件  
  323.             FileCount++;  
  324.             printf("%s     文本文件./n",dir->fcb[i].fname);  
  325.         }  
  326.         if(dir->fcb[i].type==DIRECTORY)  
  327.         {   //查找目录文件  
  328.             DirCount++;  
  329.             printf("%s     文件夹./n",dir->fcb[i].fname);  
  330.         }  
  331.     }  
  332.     printf("/n该目录下共有 %d 个文本文件, %d 个文件夹/n/n",FileCount,DirCount);  
  333.     printf("--------------------------------------------------------/n/n");  
  334.     return 1;  
  335. }  
  336.   
  337.   
  338.   
  339. /*---------在当前目录下删除文件-----------*/  
  340. int delfile(char *name)  
  341. {  
  342.     int i,temp,j;  
  343.     //确保当前目录下有该文件,并且记录下它的FCB下标  
  344.     struct dirFile *dir;     //当前目录的指针  
  345.     if(current==2)  
  346.         dir=&(osPoint->root);  
  347.     else  
  348.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  349.   
  350.     for(i=1;i<BlockFcbCount;i++) //查找该文件  
  351.     {  
  352.         if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,name)==0)  
  353.         {  
  354.             break;  
  355.         }  
  356.     }  
  357.   
  358.     if(i==BlockFcbCount)  
  359.     {  
  360.         printf("当前目录下不存在该文件!/n");  
  361.         return 0;  
  362.     }  
  363.   
  364.     int k;  
  365.     for(k=0;k<OPEN_MAX;k++)  
  366.     {  
  367.         if((openlist->f [k].type = GENERAL)&&  
  368.             (strcmp(openlist->f [k].fname,name)==0))  
  369.         {  
  370.             if(openlist->f[k].fatherBlockNum == current)  
  371.             {  
  372.                 break;  
  373.             }  
  374.             else  
  375.             {  
  376.                 printf("该文件未在当前目录下!/n");  
  377.                 return 0;  
  378.             }  
  379.         }  
  380.     }  
  381.   
  382.     if(k!=OPEN_MAX)  
  383.     {  
  384.         close(name);  
  385.     }  
  386.   
  387.     //从打开列表中删除  
  388.       
  389.     temp=i;  
  390.     /*开始删除文件操作*/  
  391.     j = dir->fcb [temp].currentBlockNum ;    //查找盘块号j  
  392.     osPoint->FAT1[j]=osPoint->FAT2[j]=0;     //fat1,fat2表标记为空白  
  393.     char *p=osPoint->data[j - 3];  
  394.     memset(p,0,BlockSize); //清除原文本文件的内容  
  395.     dir->fcb[temp].initialize();    //type=0;     //标记该目录项为空文件  
  396.     printf("------------------------------------------------------------/n/n");  
  397.     return 1;  
  398. }  
  399.   
  400.   
  401. /*--------------进入当前目录下的子目录--------------*/  
  402. int changePath(char *sonfname)  
  403. {  
  404.     struct dirFile *dir;     //当前目录的指针  
  405.     if(current==2)  
  406.         dir=&(osPoint->root);  
  407.     else  
  408.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  409.     /*回到父目录*/  
  410.     if(strcmp(sonfname,"..")==0)  
  411.     {  
  412.         if(current==2)  
  413.         {  
  414.             printf("你现已经在根目录下!/n");  
  415.             return 0;  
  416.         }  
  417.         current = dir->fcb[0].fatherBlockNum ;  
  418.         currentPath = currentPath.substr(0,currentPath.size() - strlen(dir->fcb[0].fname )-1);  
  419.         return 1;  
  420.     }  
  421.     /*进入子目录*/  
  422.     int i,temp;  
  423.     //确保当前目录下有该目录,并且记录下它的FCB下标  
  424.     for(i = 1; i < BlockFcbCount; i++)  
  425.     {     //查找该文件  
  426.         if(dir->fcb[i].type==DIRECTORY && strcmp(dir->fcb[i].fname,sonfname)==0 )  
  427.         {  
  428.             temp=i;  
  429.             break;  
  430.         }  
  431.     }  
  432.   
  433.     if(i==BlockFcbCount)  
  434.     {  
  435.         printf("不存在该目录!/n");  
  436.         return 0;  
  437.     }  
  438.   
  439.     //修改当前文件信息  
  440.     current=dir->fcb [temp].currentBlockNum ;  
  441.     currentPath = currentPath+dir->fcb [temp].fname +"//";  
  442.     printf("-------------------------------------------------------------/n/n");  
  443.     return 1;  
  444. }  
  445.   
  446. /*--------System exit---------------------*/  
  447. int exit()  
  448. {  
  449.     //将所有文件都关闭  
  450.   
  451.     //保存到磁盘上C:/myfiles  
  452.     fp=fopen(FilePath,"w+");  
  453.     fwrite(BaseAddr,sizeof(char),DiskSize,fp);  
  454.     fclose(fp);  
  455.     //释放内存上的虚拟磁盘  
  456.     free(osPoint);  
  457.     //释放用户打开文件表  
  458.     delete openlist;  
  459.     printf("---------------------------------------------------------/n/n");  
  460.     return 1;  
  461. }  
  462.   
  463. /*-------------在指定的文件里记录信息---------------*/  
  464. int write(char *name)  
  465. {  
  466.     int i;  
  467.     char *startPoint,*endPoint;  
  468.     //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!)  
  469.     for(i=0;i<OPEN_MAX;i++)  
  470.     {  
  471.         if(strcmp(openlist->f [i].fname,name)==0 )  
  472.         {  
  473.             if(openlist->f[i].fatherBlockNum ==current)  
  474.             {  
  475.                 break;  
  476.             }  
  477.             else  
  478.             {  
  479.                 printf("该文件处于打开列表中,本系统只能改写当前目录下文件!/n");  
  480.                 return 0;  
  481.             }  
  482.         }  
  483.     }  
  484.   
  485.     if(i==OPEN_MAX)  
  486.     {  
  487.         printf("该文件尚未打开,请先打开后写入信息!!/n");  
  488.         return 0;  
  489.     }  
  490.   
  491.     int active=i;  
  492.     int fileStartNum = openlist->f[active].currentBlockNum - 3 ;  
  493.     startPoint = osPoint->data[fileStartNum];  
  494.     endPoint = osPoint->data[fileStartNum + 1];  
  495.   
  496.     printf("请输入文本以Ctrl D号结束:/t");  
  497.   
  498.     char input;  
  499.     while(((input=getchar())!=4))  
  500.     {  
  501.         if(startPoint < endPoint-1)  
  502.         {  
  503.             *startPoint++ = input;  
  504.         }  
  505.         else  
  506.         {  
  507.             printf("达到单体文件最大容量!");  
  508.             *startPoint++ = 4;  
  509.             break;  
  510.         }  
  511.     }  
  512.     return 1;  
  513. }  
  514.   
  515. /*---------选择一个打开的文件读取信息----------*/  
  516. int  read(char *file)  
  517. {  
  518.     int i,fileStartNum;  
  519.     char *startPoint,*endPoint;  
  520.     //struct dirFile *dir;  
  521.     //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!)  
  522.     for(i=0;i<OPEN_MAX;i++)  
  523.     {  
  524.         if(strcmp(openlist->f [i].fname,file)==0 )  
  525.         {  
  526.             if(openlist->f[i].fatherBlockNum ==current)  
  527.             {  
  528.                 break;  
  529.             }  
  530.             else  
  531.             {  
  532.                 printf("该文件处于打开列表中,本系统只能阅读当前目录下文件!/n");  
  533.                 return 0;  
  534.             }  
  535.         }  
  536.     }  
  537.   
  538.     if(i==OPEN_MAX)  
  539.     {  
  540.         printf("该文件尚未打开,请先打开后读取信息!/n");  
  541.         return 0;  
  542.     }  
  543.     int active=i;  
  544.   
  545.     //计算文件物理地址  
  546.     fileStartNum = openlist->f[active].currentBlockNum - 3 ;  
  547.     startPoint = osPoint->data[fileStartNum];  
  548.     endPoint = osPoint->data[fileStartNum + 1];  
  549.     //end_dir=(struct dirFile *)[BlockSize-1];  
  550.   
  551.     //q=(char *)end_dir;  
  552.   
  553.     printf("该文件的内容为:  ");  
  554.     while((*startPoint)!=4&& (startPoint < endPoint))  
  555.     {  
  556.         putchar(*startPoint++);  
  557.     }  
  558.   
  559.   
  560.     printf("/n");  
  561.     return 1;  
  562.   
  563.   
  564. }  
  565. /*当前目录下添加一个打开文件*/  
  566. int open(char *file)//打开文件  
  567. {  
  568.     int i,FcbIndex;  
  569.     //确保没有打开过该文件 = 相同名字 + 相同目录  
  570.     for(i=0;i<OPEN_MAX;i++)  
  571.     {  
  572.         if(openlist->f[i].type ==GENERAL && strcmp(openlist->f [i].fname,file)==0 &&openlist->f[i].fatherBlockNum == current)  
  573.         {  
  574.             printf("该文件已经被打开!/n");  
  575.             return 0;  
  576.         }  
  577.     }  
  578.   
  579.     //确保有空的打开文件项  
  580.     if(openlist->files == OPEN_MAX)  
  581.     {  
  582.         printf("打开文件数目达到上限!无法再打开新文件./n");  
  583.         return 0;  
  584.     }  
  585.   
  586.     //确保当前目录下有该文件,并且记录下它的FCB下标  
  587.     struct dirFile *dir;     //当前目录的指针  
  588.     if(current==2)  
  589.         dir=&(osPoint->root);  
  590.     else  
  591.         dir=(struct dirFile *)(osPoint->data [current-3]);  
  592.   
  593.     for(i = 1;i< BlockFcbCount;i++)  
  594.     {     //查找该文件  
  595.         if(dir->fcb[i].type==GENERAL && strcmp(dir->fcb[i].fname,file)==0 )  
  596.         {  
  597.             FcbIndex=i;  
  598.             break;  
  599.         }  
  600.     }  
  601.   
  602.     if(i==BlockFcbCount)  
  603.     {  
  604.         printf("当前目录下不存在该文件!/n");  
  605.         return 0;  
  606.     }  
  607.   
  608.     //装载新文件进入打开文件列表,(FCB信息,文件数++) ??难道名字过不来?  
  609.     openlist->f[OpenFileCount] = dir->fcb[FcbIndex]; //FCB拷贝  
  610.     openlist->files ++;  
  611.     printf("文件打开成功!/n");  
  612.     OpenFileCount++;  
  613.     return 1;  
  614. }  
  615.   
  616. int close(char *file)  
  617. {  
  618.     //释放该文件所占内存  
  619.     //释放用户打开文件列表表项  
  620.     int i;  
  621.     //在打开文件列表中查找 file(还需要考虑同名不同目录文件的情况!!!)  
  622.     for(i=0;i<OPEN_MAX;i++)  
  623.     {  
  624.         if((openlist->f [i].type = GENERAL)&&  
  625.             (strcmp(openlist->f [i].fname,file)==0))  
  626.         {  
  627.             if(openlist->f[i].fatherBlockNum == current)  
  628.             {  
  629.                 break;  
  630.             }  
  631.             else  
  632.             {  
  633.                 printf("该文件已打开,但未在当前目录下,无法关闭!/n");  
  634.                 return 0;  
  635.             }  
  636.         }  
  637.     }  
  638.   
  639.     if(i==OPEN_MAX)  
  640.     {  
  641.         printf("该文件未在打开列表中!/n");  
  642.         return 0;  
  643.     }  
  644.   
  645.     int active=i;  
  646.     openlist->files --;  
  647.     openlist->f[active].initialize();  
  648.     OpenFileCount--;  
  649.     printf("该文件已关闭!/n");  
  650.     return 1;  
  651. }  
  652.   
  653. void main()  
  654. {  
  655.   
  656.     /*********************************************************************/  
  657.     printf("-----Welcome To My Operate System Of File(FAT)-----/n");  
  658.     //使用说明书  
  659.     printf("/n  以下是使用说明书:/n");  
  660.     printf("--------------------------------------------------------------/n");  
  661.     printf("|| format :对磁盘格式化.                           || /n");  
  662.     printf("|| exit   :安全退出该文件系统,保存信息.            || /n");  
  663.     printf("|| mkdir dirname :创建子目录.                      || /n");  
  664.     printf("|| rmdir dirname :删除子目录.                      || /n");  
  665.     printf("|| ls    dirname :显示当前目录下信息.              || /n");  
  666.     printf("|| cd     dirname :更改当前目录.                   || /n");  
  667.     printf("|| create filename :创建一个新文件,并且打开.       || /n");  
  668.     printf("|| write filename :选择一个打开的文件写入信息      || /n");  
  669.     printf("|| read   filename :选择一个打开的文件读取信息.    || /n");  
  670.     printf("|| rm     filename :删除文件.                      || /n");  
  671.     printf("|| open   filename :打开文件.                      || /n");  
  672.     printf("|| close filename :关闭文件.                       || /n");  
  673.     printf("-------------------------------------------------------------/n/n");  
  674.     //创建用户文件打开表  
  675.     openlist=new OPENLIST;  
  676.     //申请虚拟空间并且初始化  
  677.     BaseAddr=(char *)malloc(DiskSize);  
  678.     //虚拟磁盘初始化  
  679.     osPoint=(struct DISK *)(BaseAddr);  
  680.     //加载磁盘文件  
  681.     if((fp=fopen(FilePath,"r"))!=NULL){  
  682.         fread(BaseAddr,sizeof(char),DiskSize,fp);  
  683.         printf("加载磁盘文件( %s )成功,现在可以进行操作了!/n/n",FilePath);  
  684.     }  
  685.     else{  
  686.         printf("这是你第一次使用该文件管理系统!/t正在初始化.../n");  
  687.         format();  
  688.         printf("初始化已经完成,现在可以进行操作了!/n/n");  
  689.     }  
  690.   
  691.     printf("--------------------------------------------------------------/n/n");  
  692.     while(1){  
  693.         cout<<currentPath;  
  694.         cin>>cmd;  
  695.         if(cmd=="format"){  
  696.             format();  
  697.         }  
  698.         else if(cmd=="mkdir"){  
  699.             cin>>command;  
  700.             mkdir(command);  
  701.         }  
  702.         else if(cmd=="rmdir"){  
  703.             cin>>command;  
  704.             rmdir(command);  
  705.         }  
  706.         else if(cmd=="ls"){  
  707.             listshow();  
  708.         }  
  709.         else if(cmd=="cd"){  
  710.             cin>>command;  
  711.             changePath(command);  
  712.         }  
  713.         else if(cmd=="create"){  
  714.             cin>>command;  
  715.             create(command);  
  716.         }  
  717.   
  718.         else if(cmd=="write"){  
  719.             cin>>command;  
  720.             write(command);  
  721.         }  
  722.         else if(cmd=="read"){  
  723.             cin>>command;  
  724.             read(command);  
  725.         }  
  726.         else if(cmd=="rm"){  
  727.             cin>>command;  
  728.             delfile(command);  
  729.         }  
  730.         else if(cmd=="open"){  
  731.             cin>>command;  
  732.             open(command);  
  733.         }  
  734.         else if(cmd=="close"){  
  735.             cin>>command;  
  736.             close(command);  
  737.         }  
  738.         else if(cmd=="exit"){  
  739.             exit();  
  740.             break;  
  741.         }  
  742.         else cout<<"无效指令,请重新输入:"<<endl;  
  743.     }  
  744.     printf("Thank you for using my file system!/n");  
  745. }  

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值