简单文件系统实现.docx

上传人:b****7 文档编号:9590046 上传时间:2023-02-05 格式:DOCX 页数:24 大小:20.16KB
下载 相关 举报
简单文件系统实现.docx_第1页
第1页 / 共24页
简单文件系统实现.docx_第2页
第2页 / 共24页
简单文件系统实现.docx_第3页
第3页 / 共24页
简单文件系统实现.docx_第4页
第4页 / 共24页
简单文件系统实现.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

简单文件系统实现.docx

《简单文件系统实现.docx》由会员分享,可在线阅读,更多相关《简单文件系统实现.docx(24页珍藏版)》请在冰豆网上搜索。

简单文件系统实现.docx

简单文件系统实现

#include

#include

#include

#include

usingnamespacestd;

#defineGENERAL1//1代表普通文件2代表目录文件0表示空文件

#defineDIRECTORY2

#defineZero0

structFCB

{

charfname[16];//文件名

chartype;//0空文件1目录文件2空文件

intsize;//文件大小

intfatherBlockNum;//当前的父目录盘块号

intcurrentBlockNum;//当前的盘块

voidinitialize()

{

strcpy(fname,"\0");

type=Zero;

size=0;

fatherBlockNum=currentBlockNum=0;

}

};

constchar*FilePath="C:

\\myfiles";/*常量设置*/

constintBlockSize=512;//盘块大小

constintOPEN_MAX=5;//能打开最多的文件数

constintBlockCount=128;//盘块数

constintDiskSize=BlockSize*BlockCount;//磁盘大小64K

constintFcbCount=BlockSize/sizeof(FCB);//目录文件的最多FCB数

intOpenFileCount=0;//统计当前打开文件数目

structOPENLIST//用户文件打开表

{

intfiles;//当前打开文件数

FCBf[OPEN_MAX];//FCB拷贝

OPENLIST()

{

files=0;

for(inti=0;i

f[i].fatherBlockNum=-1;//为分配打开

f[i].type=GENERAL;

}

}

};

structdirFile/*-------------目录文件结构---------------*/

{

structFCBfcb[FcbCount];

voidinit(int_FatherBlockNum,int_CurrentBlockNum,char*name)//父块号,当前块号,目录名

{

strcpy(fcb[0].fname,name);//本身的FCB

fcb[0].fatherBlockNum=_FatherBlockNum;

fcb[0].currentBlockNum=_CurrentBlockNum;

fcb[0].type=DIRECTORY;//标记目录文件

for(inti=1;i

fcb[i].fatherBlockNum=_CurrentBlockNum;//标记为子项

fcb[i].type=Zero;//标记为空白项

}

}

};

structDISK/**********************************************************************/

{

intFAT1[BlockCount];//FAT1

intFAT2[BlockCount];//FAT2

structdirFileroot;//根目录

chardata[BlockCount-3][BlockSize];

voidformat(){

memset(FAT1,0,BlockCount);//FAT1清零

memset(FAT2,0,BlockCount);//FAT2清零

FAT1[0]=FAT1[1]=FAT1[2]=-2;//0,1,2盘块号依次代表FAT1,FAT2,根目录区

FAT2[0]=FAT2[1]=FAT2[2]=-2;//FAT作备份

root.init(2,2,"C:

\\");//根目录区

memset(data,0,sizeof(data));//数据区清零

}

};

FILE*fp;//磁盘文件地址

char*BaseAddr;//虚拟磁盘空间基地址

stringcurrentPath="C:

\\";//当前路径

intcurrent=2;//当前目录的盘块号

stringcmd;//输入指令

structDISK*osPoint;//磁盘操作系统指针

charcommand[16];//文件名标识

structOPENLIST*openlist;//用户文件列表指针

intformat();

intmkdir(char*sonfname);

intrmdir(char*sonfname);

intcreate(char*name);

intlistshow();

intdelfile(char*name);

intchangePath(char*sonfname);

intwrite(char*name);

intexit();

intopen(char*file);

intclose(char*file);

intread(char*file);

/*------------初始化-----------------------*/

intformat()

{

current=2;

currentPath="C:

\\";//当前路径

osPoint->format();//打开文件列表初始化

deleteopenlist;

openlist=newOPENLIST;

/*-------保存到磁盘上myfiles--------*/

fp=fopen(FilePath,"w+");

fwrite(BaseAddr,sizeof(char),DiskSize,fp);

fclose(fp);

printf("格式化成功!

\n");

return1;

}

intmkdir(char*sonfname)/*-----------------------创建子目录-------------------*/

{

//判断是否有重名、寻找空白子目录项、寻找空白盘块号、当前目录下增加该子目录项、分配子目录盘块并且初始化、修改fat表

inti,temp,iFAT;

structdirFile*dir;//当前目录的指针

if(current==2)//根目录

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

/*--------为了避免该目录下同名文件夹--------*/

for(i=1;i

if(dir->fcb[i].type==DIRECTORY&&strcmp(dir->fcb[i].fname,sonfname)==0){

printf("该文件夹下已经有同名的文件夹存在了!

\n");

return0;

}

}

for(i=1;i

if(dir->fcb[i].type==Zero)

break;

}

if(i==FcbCount){

printf("该目录已满!

请选择新的目录下创建!

\n");

return0;

}

temp=i;

for(i=3;i

if(osPoint->FAT1[i]==0)

break;

}

if(i==BlockCount){

printf("磁盘已满!

\n");

return0;

}

iFAT=i;

/*-------------接下来进行分配----------*/

osPoint->FAT1[iFAT]=osPoint->FAT2[iFAT]=2;//2表示分配给下级目录文件

//填写该分派新的盘块的参数

strcpy(dir->fcb[temp].fname,sonfname);

dir->fcb[temp].type=DIRECTORY;

dir->fcb[temp].fatherBlockNum=current;

dir->fcb[temp].currentBlockNum=iFAT;

//初始化子目录文件盘块

dir=(structdirFile*)(osPoint->data[iFAT-3]);//定位到子目录盘块号

dir->init(current,iFAT,sonfname);//iFAT是要分配的块号,这里的current用来指要分配的块的父块号

printf("创建子目录成功!

\n");

return1;

}

intrmdir(char*sonfname)/*-------删除当前目录下的文件夹--------*/

{

inti,temp,j;//确保当前目录下有该文件,并记录下该FCB下标

structdirFile*dir;//当前目录的指针

if(current==2)

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

for(i=1;i

if(dir->fcb[i].type==DIRECTORY&&strcmp(dir->fcb[i].fname,sonfname)==0){

break;

}

}

temp=i;

if(i==FcbCount){

printf("当前目录下不存在该子目录!

\n");

return0;

}

j=dir->fcb[temp].currentBlockNum;

structdirFile*sonDir;//当前子目录的指针

sonDir=(structdirFile*)(osPoint->data[j-3]);

for(i=1;i

if(sonDir->fcb[i].type!

=Zero)

{

printf("该文件夹为非空文件夹,为确保安全,请清空后再删除!

\n");

return0;

}

}

/*开始删除子目录操作*/

osPoint->FAT1[j]=osPoint->FAT2[j]=0;//fat清空

char*p=osPoint->data[j-3];//格式化子目录

memset(p,0,BlockSize);

dir->fcb[temp].initialize();//回收子目录占据目录项

printf("删除当前目录下的文件夹成功\n");

return1;

}

/*-----------在当前目录下创建文本文件---------------*/

intcreate(char*name){

inti,iFAT;//temp,

intemptyNum=0,isFound=0;//空闲目录项个数

structdirFile*dir;//当前目录的指针

if(current==2)

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

for(i=1;i

//为了避免同名的文本文件

{

if(dir->fcb[i].type==Zero&&isFound==0){

emptyNum=i;

isFound=1;

}

elseif(dir->fcb[i].type==GENERAL&&strcmp(dir->fcb[i].fname,name)==0){

printf("无法在同一目录下创建同名文件!

\n");

return0;

}

}

if(emptyNum==0){

printf("已经达到目录项容纳上限,无法创建新目录!

\n");

return0;

}

for(i=3;i

{

if(osPoint->FAT1[i]==0)

break;

}

if(i==BlockCount){

printf("磁盘已满!

\n");

return0;

}

iFAT=i;

/*------进入分配阶段---------*///分配磁盘块

osPoint->FAT1[iFAT]=osPoint->FAT2[iFAT]=1;

/*-----------接下来进行分配----------*///填写该分派新的盘块的参数

strcpy(dir->fcb[emptyNum].fname,name);

dir->fcb[emptyNum].type=GENERAL;

dir->fcb[emptyNum].fatherBlockNum=current;

dir->fcb[emptyNum].currentBlockNum=iFAT;

dir->fcb[emptyNum].size=0;

char*p=osPoint->data[iFAT-3];

memset(p,0,BlockSize);

printf("在当前目录下创建文本文件成功!

\n");

return1;

}

/*-------查询子目录------------*/

intlistshow()

{

inti,DirCount=0,FileCount=0;

//搜索当前目录

structdirFile*dir;//当前目录的指针

if(current==2)

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

for(i=1;i

if(dir->fcb[i].type==GENERAL){//查找普通文件

FileCount++;

printf("%s文本文件.\n",dir->fcb[i].fname);

}

if(dir->fcb[i].type==DIRECTORY){//查找目录文件

DirCount++;

printf("%s文件夹.\n",dir->fcb[i].fname);

}

}

printf("\n该目录下共有%d个文本文件,%d个文件夹\n\n",FileCount,DirCount);

return1;

}

/*---------在当前目录下删除文件-----------*/

intdelfile(char*name)

{

inti,temp,j;

//确保当前目录下有该文件,并且记录下它的FCB下标

structdirFile*dir;//当前目录的指针

if(current==2)

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

for(i=1;i

{

if(dir->fcb[i].type==GENERAL&&strcmp(dir->fcb[i].fname,name)==0){

break;

}

}

if(i==FcbCount){

printf("当前目录下不存在该文件!

\n");

return0;

}

intk;

for(k=0;k

if((openlist->f[k].type=GENERAL)&&

(strcmp(openlist->f[k].fname,name)==0)){

if(openlist->f[k].fatherBlockNum==current){

break;

}

else{

printf("该文件未在当前目录下!

\n");

return0;

}

}

}

if(k!

=OPEN_MAX){

close(name);

}

//从打开列表中删除/*开始删除文件操作*/

temp=i;

j=dir->fcb[temp].currentBlockNum;//查找盘块号j

osPoint->FAT1[j]=osPoint->FAT2[j]=0;//fat1,fat2表标记为空白

char*p=osPoint->data[j-3];

memset(p,0,BlockSize);//清除原文本文件的内容

dir->fcb[temp].initialize();//type=0;//标记该目录项为空文件

printf("在当前目录下删除文件成功!

\n");

return1;

}

/*--------------进入当前目录下的子目录--------------*/

intchangePath(char*sonfname)

{

structdirFile*dir;//当前目录的指针

if(current==2)

dir=&(osPoint->root);

else

dir=(structdirFile*)(osPoint->data[current-3]);

/*回到父目录*/

if(strcmp(sonfname,"..")==0){

if(current==2){

printf("你现已经在根目录下!

\n");

return0;

}

current=dir->fcb[0].fatherBlockNum;

currentPath=currentPath.substr(0,currentPath.size()-strlen(dir->fcb[0].fname)-1);

return1;

}

/*进入子目录*/

//确保当前目录下有该目录,并且记录下它的FCB下标

inti,temp;

for(i=1;i

if(dir->fcb[i].type==DIRECTORY&&strcmp(dir->fcb[i].fname,sonfname)==0){

temp=i;

break;

}

}

if(i==FcbCount){

printf("不存在该目录!

\n");

return0;

}

//修改当前文件信息

current=dir->fcb[temp].currentBlockNum;

currentPath=currentPath+dir->fcb[temp].fname+"\\";

printf("进入当前目录下的子目录成功!

\n");

return1;

}

intexit(){

//保存到磁盘上C:

\myfiles

//将所有文件都关闭

/*--------Systemexit---------------------*/

fp=fopen(FilePath,"w+");

fwrite(BaseAddr,sizeof(char),DiskSize,fp);

fclose(fp);

//释放内存上的虚拟磁盘

free(osPoint);

//释放用户打开文件表

deleteopenlist;

printf("退出文件系统成功!

\n\n");

return1;

}

intwrite(char*name)/*-------------在指定的文件里记录信息---------------*/

{

inti;

char*startPoint,*endPoint;

//在打开文件列表中查找file(还需要考虑同名不同目录文件的情况!

for(i=0;i

if(strcmp(openlist->f[i].fname,name)==0){

if(openlist->f[i].fatherBlockNum==current){

break;

}

else{

printf("该文件处于打开列表中,本系统只能改写当前目录下文件!

\n");

return0;

}

}

}

if(i==OPEN_MAX){

printf("该文件尚未打开,请先打开后写入信息!

!

\n");

return0;

}

intactive=i;

intfileStartNum=openlist->f[active].currentBlockNum-3;

startPoint=osPoint->data[fileStartNum];

endPoint=osPoint->data[fileStartNum+1];

printf("请输入文本以CtrlD号结束:

\t");

charinput;

while(((input=getchar())!

=4)){

if(startPoint

*startPoint++=input;

}

else{

printf("达到单体文件最大容量!

");

*startPoint++=4;

break;

}

}

return1;

}

intread(char*file)/*---------选择一个打开的文件读取信息----------*/

{

inti,fileStartNum;

char*startPoint,*en

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1