📄 简单文件系统.cpp
字号:
if(strcmp(name,current_cflist[i].F->fname)==0)
break;
if(i==num_oflist)
return -1;
else
{
for(j=i+1;j<num_oflist;j++)
current_cflist[j-1] = current_cflist[j];
num_oflist--;
}
ls();
return 1;
}
int Mkdir()
{
char name[20];
scanf("%s",name);
int f_d;
for(int i=0;i<fat_size;i++)
if(*(pfat+i)==blanked)
break;
*(pfat+i) = fend;//将第i号盘块设置成已用
f_d = i;
int temp = current_cfile->fd;
while(*(pfat+temp)!=fend)
temp = *(pfat+temp);//找到文件的最后一个盘块
if(current_cfile->fw+sizeof(FCB)>pan_size)//当前文件存放不下新建文件的FCB
{
for(int j=0;j<fat_size;j++)
if(*(pfat+j)==blanked)//寻找一空闲盘块
break;
*(pfat+temp)=j;//将该空闲盘块链接到文件原最后盘块之后以供使用
*(pfat+j)=fend;//将该空闲盘块设置成当前文件的最后一个盘块
current_cfile->fw = 0;//设置盘块内偏移量为0
current_cfile->flength++;//更新文件当前长度
temp = j;//令temp指向最后一个盘块
}
FCB *f = (FCB*)(ptd+(temp)*pan_size+current_cfile->fw);//计算存放新建FCB的物理位置
current_cfile->fw = current_cfile->fw + sizeof(FCB);//更新当前文件盘块内偏移量
for(i=0;i<num_dir;i++)
if(strcmp(current_cfile->fname,current_dir[i].fname)==0)
current_dir[i] = *(current_cfile); //更新当前目录再当前目录中表中的信息
f->fd = f_d;//填入新建文件FCB信息
f->flength = 1;
strcpy(f->fname,name);
f->ftype = 0;//目录型FCB
f->fw = 0;
ls();
return 1;
}
int Rmdir(char *name)
{
int i,flag;
Close(name);
int temp = current_cfile->fd;//当前目录文件的第一个盘块号
while(*(pfat+temp)!=fend)
{
FCB *OR = (FCB*)(*(ptd+temp*pan_size));//找到盘块号的物理地址
for(i=0;i<num_fcb;i++)
{
if(strcmp(name,(OR+i)->fname)==0)
{
Recycle_pan(OR+i);//回收待删除文件盘块
for(int j=i+1;j<num_fcb;j++)
*(OR+j-1) = *(OR+j);
current_cfile->fw -= sizeof(FCB);
for(i=0;i<num_dir;i++)
if(strcmp(current_cfile->fname,current_dir[i].fname)==0)
current_dir[i] = *(current_cfile); //更新当前目录再当前目录中表中的信息
flag = 1;
break;
}
}
if(flag!=1)
temp = *(pfat+temp);//如果在当前盘块上没有找到待打开文件,则转到下一盘块去搜索
else
break;
}
if(flag!=1)
{
FCB *OR = (FCB*)((ptd+temp*pan_size));//找到盘块号的物理地址
for(i=0;i<num_fcb;i++)
{
if(strcmp(name,(OR+i)->fname)==0)
{
Recycle_pan(OR+i);//回收待删除文件盘块
for(int j=i+1;j<num_fcb;j++)
*(OR+j-1) = *(OR+j);//覆盖待删除文件的FCB
current_cfile->fw -= sizeof(FCB);//更新父亲文件的盘块内偏移量
flag = 1;
break;
}
}
}
if(flag!=1)
printf("\nThe file has not exit!\n");
ls();
return 1;
}
int cd()
{
char name[20];
char e[] = "..";
scanf("%s",name);
if(strcmp(name,e)==0)
{
//printf("%d\n",num_dir);
current_cfile = ¤t_dir[num_dir-2];//倒数第二个目录FCB
num_dir--;
ls();
}
else
{
int i,flag;
int temp = current_cfile->fd;//当前目录文件的第一个盘块号
while(*(pfat+temp)!=fend)
{
FCB *OR = (FCB*)(*(ptd+temp*pan_size));//找到盘块号的物理地址
for(i=0;i<num_fcb;i++)
{
if(strcmp(name,(OR+i)->fname)==0)
{
current_dir[num_dir++] = *(OR+i);
current_cfile = OR+i;
flag = 1;
break;
}
}
if(flag!=1)
temp = *(pfat+temp);//如果在当前盘块上没有找到待打开文件,则转到下一盘块去搜索
else
break;
}
if(flag!=1)
{
FCB *OR = (FCB*)((ptd+temp*pan_size));//找到盘块号的物理地址
for(i=0;i<num_fcb;i++)
{
if(strcmp(name,(OR+i)->fname)==0)
{
current_dir[num_dir++] = *(OR+i);
current_cfile = OR+i;
flag = 1;
break;
}
}
}
if(flag!=1)
printf("\nTt's a bad commend!\n");
ls();
}
return 1;
}
int Wirte(char *name,char *buffer,int count)
{
int i,start,np,temp,l;
CURRENT_CFLIST *T;
for(i=0;i<num_oflist;i++)
if(strcmp(current_cflist[i].F->fname,name)==0)
{ T = &(current_cflist[i]);
break;
}
if(i==num_oflist)
{ printf("\nThe file is not exit!\n");
ls();
return 0;
}
np = count/pan_size;
l = T->F->flength;
if(l<np)
{
i=np-l;
temp = T->F->fd;
while(*(pfat+temp)!=fend)
temp = *(pfat+temp);
while(i<np)
{
for(int j=0;j<fat_size;j++)
if(*(pfat+j)==blanked)
{
*(pfat+temp) = j;
*(pfat+j) = fend;
temp = j;
break;
}
}
}
int temppan=T->F->fd;
int j=0;
for(i=0;i<count;i++,j++)
{
if(j==pan_size)
{
temppan = *(pfat+temppan);
j = 0;
}
*(ptd+temppan*pan_size+j) = *(buffer+i);
}
T->rw = count;
if(l<np)
T->F->flength += np-l;
return count;
}
int Read(char *name,char *buffer,int count)
{
int i,start,np,temp,l;
CURRENT_CFLIST *T;
for(i=0;i<num_oflist;i++)
if(strcmp(current_cflist[i].F->fname,name)==0)
{
T = &(current_cflist[i]);
break;
}
if(i==num_oflist)
{ printf("\nThe file is not exit!\n");
ls();
return 0;
}
np = count/pan_size;
int temppan=T->F->fd;
int j=0;
for(i=0;i<count;i++,j++)
{
if(j==pan_size)
{
temppan = *(pfat+temppan);
j = 0;
}
*(buffer+i) = *(ptd+temppan*pan_size+j);
}
return 1;
}
int Enter_sy()
{
FILE *mf = fopen("myfilesy.txt","rb");
pfile = (char*)malloc(sizeof(char)*all_size);
fread(pfile,1,all_size,mf);
return 1;
}
int Out_of_sy(char *p)
{
FILE *mf = fopen("myfilesy.txt","wb");
int *temp = pfat+fat_size;
for(int i=0;i<fat_size;i++)//更新备份FAT表
*(temp+i)=*(pfat+i);
fwrite(p,sizeof(char),all_size,mf);
free(p);
return 0;
}
int main()
{
char commend1[]="ls";
char commend2[]="cfile";
char commend3[]="creat";
char commend4[] = "open";
char commend5[] = "out";
char commend6[] = "format";
char commend7[] = "close";
char commend8[] = "delete";
char commend9[] = "dir";
char commend10[] = "mkdir";
char commend11[] = "rmdir";
char commend12[] = "cd";
char commend13[] = "write";
char commend14[] = "read";
char commend[10];
Enter_sy();
Initsy(pfile);
while(scanf("%s",commend))
{
if(strcmp(commend,commend1)==0)
ls();
else if(strcmp(commend,commend2)==0)
cfile();
else if(strcmp(commend,commend3)==0)
Creat();
else if(strcmp(commend,commend4)==0)
{
char tname[20];
scanf("%s",tname);
Open(tname);
}
else if(strcmp(commend,commend5)==0)
{
Out_of_sy(pfile);
break;
}
else if(strcmp(commend,commend6)==0)
Format();
else if(strcmp(commend,commend7)==0)
{
char tname[20];
scanf("%s",tname);
Close(tname);
}
else if(strcmp(commend,commend8)==0)
{
char tname[20];
scanf("%s",tname);
Delete(tname);
}
else if(strcmp(commend,commend9)==0)
{
Dir();
}
else if(strcmp(commend,commend10)==0)
{
Mkdir();
}
else if(strcmp(commend,commend11)==0)
{
char tname[20];
scanf("%s",tname);
Rmdir(tname);
}
else if(strcmp(commend,commend12)==0)
{
cd();
}
else if(strcmp(commend,commend13)==0)
{
char buffer[12],char tname[20];
scanf("%s",tname);
scanf("%s",buffer);
Wirte(tname,buffer,10);
}
else if(strcmp(commend,commend14)==0)
{
char buffer[12],char tname[20];
scanf("%s",tname);
Read(tname,buffer,10);
printf("%s",buffer);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -