操作系统 文件与磁盘管理 C语言(模拟文件目录)

时间:2025-03-03 08:34:30
#include <> #include <> #include<> #include <> typedef struct FCB{ //文件或目录控制块 char name[10];//文件或目录名 int size;//文件或目录大小,目录大小可设置为 0 char type;//类型,1 为文件,2 为目录 int first;//外存起始位置 -1被占 -2未被占 char datetime[128]; //日期时间,格式为 yyyymmdd hhmmss struct FCB *next;//下一个兄弟节点 struct FCB *child;//第一个孩子节点 struct FCB *parent; }F; typedef struct list{ char name[10]; struct node *next; }node; F *p = NULL;//指向当前目录 int b[100][100];//位视图 int FAT[100]; node *head = NULL; void getTime(F *f) { time_t t; char buf[128]; memset(buf,0,sizeof(buf)); struct tm *tmp; t = time(NULL); tmp = localtime(&t); strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S",tmp); for(int i = 0;i<128 ; i++) { f->datetime[i] = buf[i]; } } void byteCreat() { srand((unsigned)time(NULL)); for(int i = 0;i<8;i++) { for(int j = 0;j<8;j++) { b[i][j]=rand()%2; printf("%d ",b[i][j]); if(b[i][j]==0) { FAT[i*8+j] = -2;//未被占; }else if(b[i][j]==1) { FAT[i*8+j] = -1;//-1:被占 } } printf("\n"); } } void byteShow() { for(int i = 0;i<8;i++) { for(int j = 0;j<8;j++) { printf("%d ",b[i][j]); } printf("\n"); } } void fatShow() { int count = 0; for(int i = 0; i<64;i++) { if(FAT[i]!=-2&&FAT[i]!=-1) { count++; printf("%d->%d\n",i,FAT[i]); if(count==2) { printf("***\n"); count=0; } } } } int findNULL() { int x,y,z; int a = 0; for(int i = 0;i<8;i++) { for(int j = 0;j<8;j++) { if(b[i][j] == 0) { x = i*8+j; b[i][j] = 1; a = 1; break; } } if(a==1) break; } a = 0; for(int i = 0;i<8;i++) { for(int j = 0;j<8;j++) { if(b[i][j] == 0) { y = i*8+j; b[i][j] = 1; a = 1; break; } } if(a==1) break; } a = 0; for(int i = 0;i<8;i++) { for(int j = 0;j<8;j++) { if(b[i][j] == 0) { z = i*8+j; b[i][j] = 1; a = 1; break; } } if(a==1) break; } FAT[x] = y; FAT[y] = z; FAT[z] = -1; return x; } void makedir(int a) { char name[10]; F *np = p; //printf("请输入目录或文件的名字\n"); scanf(" %s",name); F *f = (F *)malloc(sizeof(F));//新建空白目录 strcpy(f->name,name); if(a==1) { f->type = 1; f->first = findNULL(); } if(a==2) { f->type = 2; f->first = -2; } f->size = 0; getTime(f); f->child = NULL; f->next = NULL; f->parent = np; if(np->child==NULL) { np->child = f; }else//进入当前子目录 { np = np->child;//指向第一个目录或文件 if(a==2) { if(np->type==2) { while(np->next!=NULL) { if(np->type==2&&np->next->type == 1)//新建在目录和文件中间 { break; } np = np->next; } f->next = np->next; np->next = f; } }else if(a==1) { while(np->next!=NULL) { np = np->next; } f->next = np->next; np->next = f; } } } void cdir(char *name) { F* np = p; np = np->child; if(strcmp(np->name,name)==0) { p = np;//找到并进入 }else { while(strcmp(np->name,name)!=0) { np = np->next; if(strcmp(np->name,name)==0) { p = np;//找到并进入 break; } if(np==NULL) { printf("未找到此目录"); break; } } } } void printNow() { F *np = p; if(p->child==NULL) { printf("当前文件夹为空\n"); }else { np = np->child; printf("%s <DIR> .\n",np->parent->datetime); printf("%s <DIR> ..\n",np->parent->datetime); while(np!=NULL) { printf("%s 类型:%d 名字:%s\n",np->datetime,np->type,np->name); np = np->next; } } } void delDir(char *name) { int flag = 0; F *np = p; if(np->child==NULL) printf("无此目录\n"); else { np = np->child; if(strcmp(np->name,name)==0&&np->child==NULL)//为第一个 { p->child=np->next; free(np); printf("成功删除此目录\n"); flag = 1; } else { while(np->next!=NULL) { if(strcmp(np->next->name,name)==0&&np->next->child==NULL) { np->next = np->next->next; flag = 1; printf("成功删除此目录\n"); break; } np = np->next; } } if(flag==0) { printf("删除失败!无当前目录或当前目录不为空\n"); } } } void delFile(char *name) { int flag = 0; F *np = p; if(np->child==NULL) printf("无此文件\n"); else { np = np->child; if(strcmp(np->name,name)==0)//为第一个 { p->child=np->next; printf("成功删除此文件\n"); updateTable(np->first);//更新fat表 flag = 1; } else { while(np->next!=NULL) { if(strcmp(np->next->name,name)==0) { updateTable(np->next->first);//更新fat表 np->next = np->next->next; flag = 1; printf("成功删除此文件\n"); break; } np = np->next; } } if(flag==0) { printf("删除失败!无当前目录或当前目录不为空\n"); } } } void updateTable(int first) { int next = first;//4 int next1 = FAT[next];//7 int next2 = FAT[next1];//11 FAT[next] = -2;//修改FAT表 FAT[next1] = -2; FAT[next2] = -2; b[next/8][next%8] = 0;//修改位视图表 b[next1/8][next1%8] = 0; b[next2/8][next2%8] = 0; } void updatelujing()//更新当前所在位置 { F *np = p; F a[100]; int count = 0; while(np->parent!=NULL) { a[count] = *np; np = np->parent; count++; } for(int i = count-1;i>0;i--) { printf("%s\\",a[i].name); } printf("%s\\",p->name); printf(">"); } int main() { char a[6]; char f_name[10]; int flag = 1; F *f = (F *)malloc(sizeof(F)); strcpy(f->name,"root"); f->type = 2; f->size = 0; getTime(f); f->child = NULL; f->next = NULL; f->parent = NULL; f->first = -2; p = f;//p指向当前目录 byteCreat(); head = (node *)malloc(sizeof(node)); head->next = NULL; while(flag) { do{ updatelujing(); scanf(" %s",a); if(strcmp(a,"md")==0)//创建空目录 { makedir(2); }else if(strcmp(a,"cd")==0) { //printf("请输入目录名\n"); scanf(" %s",f_name); cdir(f_name); }else if(strcmp(a,"rd")==0)//删除空目录 { //printf("请输入需要删除的目录名\n"); scanf(" %s",f_name); delDir(f_name); }else if(strcmp(a,"mk")==0)//创建文件 { makedir(1); }else if(strcmp(a,"del")==0)//删除文件 { //printf("请输入需要删除的文件名\n"); scanf(" %s",f_name); delFile(f_name); }else if(strcmp(a,"dir")==0)//列出目录信息 { printNow(); }else if(strcmp(a,"q")==0) { flag = 0; }else if(strcmp(a,"show") == 0)//展示位示图 { byteShow(); }else if(strcmp(a,"fat") == 0)//展示fat表 { fatShow(); }else if(strcmp(a,"cd..") == 0)//返回上一级目录 { p = p->parent; } }while(flag==1); } return 0; }