📄 文件系统实现.c
字号:
str1=strchr(str,'\\');
//free(temp);
}
str1=str1+strlen(str);
for(i=0;i<(int)strlen(str);i++)
temp[i]=str[i];
temp[i]='\0';
for(j=0;j<MSD+2;j++) /*-查找该子目录是否在当前目录中-*/
{
if(!strcmp(temp_dir->directitem[j].name,temp))
break;
}
free(temp);/*释放申请的临时空间*/
if(temp_dir->directitem[j].property!='1') /*-打开的不是目录-*/
return(-2);
if(j>=MSD+2) /*-不在当前目录-*/
return(-1);
item=temp_dir->directitem[j].firstdisk;
/*-当前目录在磁盘中位置-*/
temp_dir=(struct direct *)(fdisk+item*DISKSIZE);
if(!strcmp("..",name))
{
if(cur_dir->directitem[j-1].sign!=1) /*-如果子目录不是根目录-*/
{
point=strchr(bufferdir,'\\');
while(point!=NULL)
{
point1=point+1; /*-减去'\'所占的空间,记录下次查找的起始地址-*/
point=strchr(point1,'\\');
}
*(point1-1)='\0'; /*-将上一级目录删除-*/
}
else
{
}
}
else if(!strcmp(".",name))
{
bufferdir=bufferdir; /*-如果是当前目录则不变-*/
}
else
{
if(name[0] !='\\')
bufferdir = strcat(bufferdir,"\\"); /*-修改当前目录-*/
bufferdir = strcat(bufferdir,name);
}
cur_dir=temp_dir; /*-将当前目录确定下来-*/
return 0;
}
/*----------------------------------------------------------------------------------------------*/
/*---------------------------------------显示当前路径-------------------------------------------*/
void show()
{
printf("%s>",bufferdir);
}
/*----------------------------------------------------------------------------------------------*/
/*--------------------------------------输出提示信息--------------------------------------------*/
void print()
{
printf("********************************************************************************\n");
printf("\t\t\tWelcome to DOS File system!\n");
printf("--------------------------------------------------------------------------------\n");
printf("\t\t 退出文件系统 halt\n");
printf("\t\t 创建文件 create 文件名\n");
printf("\t\t 删除文件 del 文件名\n");
printf("\t\t 打开文件 open 文件名\n");
printf("\t\t 关闭文件 close 文件名\n");
printf("\t\t 写文件 write\n");
printf("\t\t 读文件 read\n\n");
printf("\t\t 创建子目录 mkdir 目录名\n");
printf("\t\t 删除子目录 rmdir 目录名\n");
printf("\t\t 显示当前目录的子目录 dir\n");
printf("\t\t 更改当前目录 cd 目录名\n");
printf("--------------------------------------------------------------------------------\n");
}
/*----------------------------------------------------------------------------------------------*/
/*------------------------------------------主函数----------------------------------------------*/
void main()
{
FILE *fp;
char ch;
char a[100];
char code[11][10];
char name[10];
int i,flag,r_size;
char *contect;
contect = (char *)malloc(MAX_WRITE*sizeof(char));
if((fp=fopen("disk.dat","rb"))==NULL)/*如果还没有进行格式化,则要格式化*/
{
printf("You have not format,Do you want format?(y/n)");
scanf("%c",&ch);
if(ch=='y')
{
initfile();
printf("Successfully format! \n");
}
else
{
return;
}
}
enter();
print();
show();
/*将命令全部保存在CODE数组中*/
strcpy(code[0],"halt");
strcpy(code[1],"create");
strcpy(code[2],"open");
strcpy(code[3],"close");
strcpy(code[4],"write");
strcpy(code[5],"read");
strcpy(code[6],"del");
strcpy(code[7],"mkdir");
strcpy(code[8],"rmdir");
strcpy(code[9],"dir");
strcpy(code[10],"cd");
while(1)
{
scanf("%s",a);
for(i=0;i<11;i++)
{
if(!strcmp(code[i],a))
break;
}
switch(i)
{
case 0: //*--退出文件系统--//
free(contect);
halt();
return;
case 1: //*--创建文件--//
scanf("%s",name);
flag = create(name);
if(flag==-1)
{
printf("Error: \n The length is too long !\n");
}
else if(flag==-2)
{
printf("Error: \n The direct item is already full !\n");
}
else if(flag==-3)
{
printf("Error: \n The number of openfile is too much !\n");
}
else if(flag==-4)
{
printf("Error: \n The name is already in the direct !\n");
}
else if(flag==-5)
{
printf("Error: \n The disk space is full!\n");
}
else
{
printf("Successfully create a file! \n");
}
show();
break;
case 2://--打开文件--//
scanf("%s",name);
fd = open(name);
if(fd == -1)
{
printf("Error: \n The open file not exit! \n");
}
else if(fd == -2)
{
printf("Error: \n The file have already opened! \n");
}
else if(fd == -3)
{
printf("Error: \n The number of open file is too much! \n");
}
else if(fd == -4)
{
printf("Error: \n It is a direct,can not open for read or write! \n");
}
else
{
printf("Successfully opened! \n");
}
show();
break;
case 3://--关闭文件--//
scanf("%s",name);
flag = close(name);
if(flag == -1)
{
printf("Error:\n The file is not opened ! \n");
}
else
{
printf("Successfully closed! \n");
}
show();
break;
case 4:/*--写文件--*/
if(fd ==-1)
{
printf("Error:\n The file is not opened ! \n");
}
else
{
printf("Please input the file contect:");
scanf("%s",contect);
flag=write(fd,contect,strlen(contect));
if(flag == 0)
{
printf("Successfully write! \n");
}
else
{
printf("Error:\n The disk size is not enough! \n");
}
}
show();
break;
case 5:/*--读文件--*/
if(fd ==-1)
{
printf("Error:\n The file is not opened ! \n");
}
else
{
flag = read(fd,contect);
if(flag == -1)
{
printf("Error: \n The size is over the length of the file! \n");
}
else
{
//printf("Successfully read! \n The contect is :");
for(i=0;i<u_opentable.openitem[fd].size;i++)
{
printf("%c",contect[i]);
}
printf("\t\n");
}
}
show();
break;
case 6://*--删除文件--
scanf("%s",name);
flag = del(name);
if(flag == -1)
{
printf("Error:\n The file not exit! \n");
}
else if(flag == -2)
{
printf("Error:\n The file is opened,please first close it ! \n");
}
else if(flag == -3)
{
printf("Error:\n The delete is not file ! \n");
}
else
{
printf("Successfully delete! \n");
}
show();
break;
case 7://*--创建子目录--/
scanf("%s",name);
flag = mkdir(name);
if(flag == -1)
{
printf("Error:\n The length of name is to long! \n");
}
else if(flag == -2)
{
printf("Error:\n The direct item is already full ! \n");
}
else if(flag == -3)
{
printf("Error:\n The name is already in the direct ! \n");
}
else if(flag == -4)
{
printf("Error:\n \\ can not in the name of a direct ! \n");
}
else if(flag == -5)
{
printf("Error: \n The disk space is full!\n");
}
else if(flag == -6)
{
printf("Error: \n '..' or '.' can not as the name of the direct!\n");
}
else if(flag == 0)
{
printf("Successfully make dircet! \n");
}
show();
break;
case 8://*--删除子目录--/
scanf("%s",name);
flag = rmdir(name);
if(flag == -1)
{
printf("Error:\n The direct is not exist! \n");
}
else if(flag == -2)
{
printf("Error:\nThe direct has son direct ,please first remove the son dircct!\n");
}
else if(flag == -3)
{
printf("Error:\n The remove is not direct ! \n");
}
else if(flag == 0)
{
printf("Successfully remove dircet! \n");
}
show();
break;
case 9://*--显示当前子目录--/
dir();
show();
break;
case 10:/*--更改当前目录--*/
scanf("%s",name);
flag = cd(name);
if(flag == -1)
{
printf("Error:\n The path no correct!\n");
}
else if(flag == -2)
{
printf("Error:\nThe opened is not direct!\n");
}
else if(flag == -3)
{
printf("Error:\nThe '\\' is too much !\n");
}
show();
break;
default:
printf("\n Error!\n The command is wrong! \n");
show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -