📄 文件系统.cpp
字号:
#include "stdio.h"
#include "string.h"
#include <stdlib.h>
#include <conio.h>
#define getone(type,n) (type*)malloc(n*sizeof(type))
#define NULL 0
#define myCreate "Create"
#define mydeLete "deLete"
#define myopen "open"
#define mybye "bye"
#define myclose "close"
#define myread "read"
#define mywrite "write"
#define myexit "exit"
char allcmd[8][10]={{myCreate},{mydeLete},{myopen},{mybye},{myclose},{myread },{mywrite},{myexit}};
struct afd {
char name[10];//打开文件名
char protect[4];//打开保护码
FILE *fp;//文件指针
}theafd;
typedef struct adf ADF;
//打开文件名,保护码,读写指针
struct fd {
char name[12];//文件名
char protect[4];//保护码
int length;//长度
//FILE *fp;//内容
};
typedef struct fd FD;
//文件名结构体,未用
struct ufd {
char name[10];//目录名
int filecount;
FD * allfile;//文件
};
typedef struct ufd UFD;
struct user {
char name[10];
UFD * myufd;
};
typedef struct user USER;
//用户名结构,未用
struct mfd {
UFD * allufd;
}themfd;
typedef struct mfd MFD;
void init ()//初始化主目录
{
themfd.allufd=getone(UFD,10);
for(int i=0;i<10;i++)
{
themfd.allufd[i].name[0]='\0';//空名字表示不存在
themfd.allufd[i].filecount=0;
themfd.allufd[i].allfile=getone(FD,10);
for(int j=0;j<10;j++)//初始化子目录中的文件,全都是空
{
themfd.allufd[i].allfile[j].name[0]='\0';
themfd.allufd[i].allfile[j].length=0;
strcpy(themfd.allufd[i].allfile[j].protect,"000");
}
}
}
void printmfd()//显示主目录中的文件
{
printf("主目录中的目录如下 \n\n");
for(int i=0;i<10;i++)
{
if(themfd.allufd[i].name[0]=='\0') continue;
printf(themfd.allufd[i].name);
printf("\n");
}
printf("\n");
}
void printufd(int ufdid)//显示指定子目录中的文件
{
printf("目录名称 %s\n目录中的文件如下: \n\n",themfd.allufd[ufdid].name);
for(int i=0;i<10;i++)
{
if(themfd.allufd[ufdid].allfile[i].name[0]=='\0') continue;
printf(themfd.allufd[ufdid].allfile[i].name);
printf("\n");
}
printf("\n");
}
void createufd()//创建一个新的子目录
{
char ufdname[13];
int inserid=10;
printf("请输入要创建的文件夹名\n");
scanf("%s",ufdname);
for(int i=0;i<10;i++)//是否存在同名文件
{
if(!strcmp(ufdname,themfd.allufd[i].name))
{
printf("error\tthe ufd is in\n");
return;
}
if(!strcmp("",themfd.allufd[i].name)) //在子目录数组中找到一个空位置
{
inserid=i;
}
}
if(inserid==10) printf("error\tthe mfd is full\n");//找不到则说明主目录已满
else
{
strcpy(themfd.allufd[inserid].name,ufdname);//改变目录名,使其不为空,以示已创建
themfd.allufd[inserid].filecount=0;
printf("创建成功\n");
printmfd();
}
}
void docreate(int ufdid)
{//在指定目录中创建一个文件,和在主目录中创建一个子目录差不多
char filename[12];
char protect[3];
int length;
int inserid=10;
if(themfd.allufd[ufdid].filecount==10)
{
printf("error\tthe ufd is full\n");
return;
}
//目录已满
printf("请输入要创建的文件名\n");
scanf("%s",filename);
printf("请输入文件长度\n");
scanf("%d",&length);
printf("请输入三位保护码\n");
scanf("%s",protect);
//不满,找出位置放文件
for(int i=0;i<10;i++)
{
if(!strcmp(filename,themfd.allufd[ufdid].allfile[i].name))
{
printf("error\tthe file is in\n");
return;
}
if(!strcmp("",themfd.allufd[ufdid].allfile[i].name))
{
inserid=i;
}
}
themfd.allufd[ufdid].allfile[inserid].length=length;
strcpy(themfd.allufd[ufdid].allfile[inserid].name,filename);
strcpy(themfd.allufd[ufdid].allfile[inserid].protect,protect);
themfd.allufd[ufdid].filecount++;
printf("创建成功\n");
printufd(ufdid);
return;
}
int dodelete(int ufdid)
{
char filename[12];
printf("请输入待删除的文件名\n");
scanf("%s",filename);
for(int i=0;i<10;i++)
{
if(!strcmp(filename,themfd.allufd[ufdid].allfile[i].name))
{//寻找待删除文件
if(themfd.allufd[ufdid].allfile[i].protect[1]=='1')
{//文件是否允许写
themfd.allufd[ufdid].allfile[i].name[0]='\0';
themfd.allufd[ufdid].allfile[i].length=0;
themfd.allufd[ufdid].allfile[i].protect[0]=0;
themfd.allufd[ufdid].allfile[i].protect[0]=0;
themfd.allufd[ufdid].allfile[i].protect[0]=0;
themfd.allufd[ufdid].filecount--;
if(themfd.allufd[ufdid].filecount==0)
{
printf("文件被删除后目录为空,程序自动删除此目录并返回主目录\n");
themfd.allufd[ufdid].name[0]='\0';
free(themfd.allufd[ufdid].allfile);
return 1;
//照我对实验要求的理解,删除文件后目录为空则将目录人、也删除
}
printf("删除成功\n");
printufd(ufdid);
//显示删除后目录的信息
}
else printf("error!\tthe file can't not be writen\n");
return 0;
}
}
printf("error!\tthe file not found\n");
return 0;
}
void doopen(int ufdid)
{
char filename[13];
printf("请输入你想要打开的文件名\n");
scanf("%s",filename);
for(int i=0;i<10;i++)
{//通过文件名查找文件,并将其信息复制到打开文件中
if(!strcmp(filename,themfd.allufd[ufdid].allfile[i].name))
{
strcpy(theafd.name,filename);
strcpy(theafd.protect,themfd.allufd[ufdid].allfile[i].protect);
theafd.fp=NULL;
return;
}
}
printf("找不到该文件\n");
}
void dobye(int ufdid)
{
printufd(ufdid);
printf("返回主目录\n");
}
void doclose()
{
if(theafd.fp!=NULL) fclose(theafd.fp);
theafd.name[0]='\0';//关闭文件,清空afd
strcpy(theafd.protect,"000");
printf("文件成功被关闭\n");
getchar();
}
void doread(int ufdid)
{
char ak;
if(theafd.name[0]=='\0') printf("请先打开一个文件再执行此操作\n");
//本程序限制只有先打开文件才能执行读写操作
else if(theafd.protect[0]=='0') printf("打开文件无法执行读操作\n");
else//文件允许读操作
{
theafd.fp=fopen(theafd.name,"a+");
//以追加方式打开,并将文件指针移到开头
rewind(theafd.fp);
printf("读取文件成功\n文件名\t保护码\n");
printf("%s\t%s\n",theafd.name,theafd.protect);
theafd.fp=fopen(theafd.name,"r");
theafd.protect[1]='0';//读打开文件不允许写
getchar();
while((ak=fgetc(theafd.fp))!=EOF)
putchar(ak);
//通过文本操作模仿读操作
}
}
void dowrite(int ufdid)
{
char ak;
if(theafd.name[0]=='\0') printf("请先打开一个文件再执行此操作\n");
//本程序限制只有先打开文件才能执行读写操作
else if(theafd.protect[1]=='0') printf("打开文件无法执行写操作\n");
else//文件允许读操作
{
printf("写打开文件成功\n文件名\t保护码\n");
printf("%s\t%s\n",theafd.name,theafd.protect);
theafd.fp=fopen(theafd.name,"w");
theafd.protect[0]='0';//读打开文件不允许写
getchar();
printf("请输入文件内容,以 # 为结束\n");
while((ak=getchar())!='#')
fputc(ak,theafd.fp);
//通过文件操作模仿写操作
}
}
void runing(int ufdid)//等待用户输入命令
{
char cmd[10];
int i,cmdid=8,flag=1;
while(flag)//如果flag不为0则循环等待用户输入命令
{
printf("等待输入命令>>\n");
scanf("%s",cmd);
for(i=0;i<8;i++)
{
if(!strcmp(cmd,allcmd[i]))//匹配命令
{
cmdid=i;
break;
}
}
switch (cmdid)//执行相应操作
{
case 0: docreate(ufdid);break;
case 1: if(dodelete(ufdid))flag=0;break;//删除文件,如果删除后为空则返回主目录mdf
case 2: doopen(ufdid);break;
case 3: dobye(ufdid);flag=0;break;//返回主目录mdf
case 4: doclose();break;//由于afd只有一个文件,所以不用参数
case 5: doread(ufdid);break;//本程序限制只有先打开文件才能执行读写操作
case 6: dowrite(ufdid);break;
case 7: free(themfd.allufd);exit(0);break;//直接退出程序
default : printf("错误的命令\n");getchar();break;
}
cmdid=8;
}
}
int main() /*主函数*/
{
char username[10],cmd[10];
int i,ufdid=10;
init();
printf("主目录中只能使用Create,cd或exit命令\n");
while(1)//循环直到用户退出
{
ufdid=10;
printf("请输入你所需要的命令\n");
scanf("%s",cmd);
if(!strcmp(cmd,myexit)) break;//退出命令
else if(!strcmp(cmd,myCreate)) createufd();//创建用户
else if(!strcmp(cmd,"cd"))//进入子目录
{
printmfd();
printf("\n请输入要进入的目录\n");
scanf("%s",username);
for(i=0;i<10;i++)//匹配目录名
{
if(!strcmp(username,themfd.allufd[i].name))
{
ufdid=i;
break;
}
}
if(ufdid==10)//找不到目录
{
printf("error\ncan't not find the user\n");
}
else//找到则初始化afd并打印目录中的信息
{
printufd(ufdid);
theafd.fp=NULL;
theafd.name[0]='\0';
strcpy(theafd.protect,"000");
runing(ufdid);
}
}
else //输入错误的命令
{
printf("错误的命令\n");
printf("主目录中只能使用Create,cd或exit命令\n");
getchar();
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -