操作系统实验文件管理C++代码.docx
- 文档编号:24729767
- 上传时间:2023-06-01
- 格式:DOCX
- 页数:18
- 大小:17.10KB
操作系统实验文件管理C++代码.docx
《操作系统实验文件管理C++代码.docx》由会员分享,可在线阅读,更多相关《操作系统实验文件管理C++代码.docx(18页珍藏版)》请在冰豆网上搜索。
操作系统实验文件管理C++代码
#include<>
#include<>
#include<>
#include<>
#include
usingnamespacestd;
#defineBLKSIZE512.\n");
printf("WARNING:
ALLDATAONTHISFILESYSTEMWILLBELOST!
\n");
printf("ProceedwithFormat(Y/N)?
");
scanf("%c",&choice);
getchar();
if((choice=='y')||(choice=='Y'))
{
if((fp=fopen(image_name,"w+b"))==NULL)
{
printf("Can'tcreatefile%s\n",image_name);
exit(-1);
}
for(i=0;i fputc('0',fp); strcpy,"/"); ='d'; strcpy,"/"); =0; =0; [0]=-1; [1]=-1; fwrite(&inode,sizeof(Inode),1,fp); =-1; for(i=0;i<31;i++)fwrite(&inode,sizeof(Inode),1,fp); for(i=0;i fclose(fp); n",file_name); be printf("Thisfilesystemnotexist,itwillcreate! \n"); format(); login(); } while(! feof(fp)) { fread(&user,sizeof(User),1,fp);n"); flag=1;fclose(fp); break; } } if(flag==0)break; }while(flag); n",image_name);exit(-1); } num=-1; } n"); exit(-1); }bitmap[i]='1'; if((fp=fopen(image_name,"r+b"))==NULL) printf("Can'topenfile%s\n",image_name); exit(-1); fseek(fp,i,SEEK_SET); fputc('1',fp); fclose(fp); returni; } ength; add0=inode_array[num].address[0]; if(len>512) add1=inode_array[num].address[1]; if((fp=fopen(image_name,"r+b"))==NULL) { printf("Can'topenfile%s.\n",image_name); exit(-1); } fseek(fp,BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE,SEEK_SET); ch=fgetc(fp); for(i=0;(i ='\0')&&(i<512);i++) temp[i]=ch; ch=fgetc(fp); if(i>=512) {fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); ch=fgetc(fp); for(;(i ='\0');i++) { temp[i]=ch; ch=fgetc(fp); } } temp[i]='\0'; fclose(fp); } ddress[0]; len=inode_array[num].length; if((fp=fopen(image_name,"r+b"))==NULL) { printf("Can'topenfile%s.\n",image_name); exit(-1); } fseek(fp,BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE,SEEK_SET); for(i=0;(i ='\0')&&(i<512);i++) fputc(temp[i],fp); if(i==512) { add1=inode_array[num].address[1]; fseek(fp, BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE,SEEK_SET); for(;(i ='\0');i++) fputc(temp[i],fp); } fputc('\0',fp); fclose(fp); } num==0) strcpy(path,; else { strcpy(path,; m=0; n=inum_cur; while(m! =inum_cur) { while(inode_array[n].iparent! =m) { n=inode_array[n].iparent; } strcat(path,"/"); strcat(path,inode_array[n].file_name);m=n; n=inum_cur; } } printf("[%s]$",path); } 或者cddir1) voidcd(void) { inti; if(argc! =2) printf("Commandcdmusthavetwoargs.\n"); if(! strcmp(argv[1],"..")) inum_cur=inode_array[inum_cur].iparent;else { for(i=0;i if((inode_array[i].inum>0)&&(inode_array[i].type=='d')&& (inode_array[i].iparent==inum_cur)&& ! strcmp(inode_array[i].file_name,argv[1])&& ! strcmp(inode_array[i].user_name,) break; if(i==INODENUM) printf("Thisdirectoryisn'texsited.\n");else inum_cur=i; \n"); } num>0)&& (inode_array[i].iparent==inum_cur)&& ! strcmp(inode_array[i].user_name,) { if(inode_array[i].type=='d') { dcount++; printf("%-20s inode_array[i].file_name); } else { fcount++; bcount+=inode_array[i].length; bytes\n", printf("%-20s%12d inode_array[i].file_name,inode_array[i].length); } } printf("\n %dfile(s)%11d bytes\n",fcount,bcount); %11dbytes printf("%ddir(s)FreeSpace\n",dcount,1024*1024-bcount); } \n"); return; } num<0)break; if(i==INODENUM) { printf("Inodeisfull.\n"); exit(-1); } inode_array[i].inum=i; strcpy(inode_array[i].file_name,argv[1]);inode_array[i].type='d'; strcpy(inode_array[i].user_name,; inode_array[i].iparent=inum_cur; inode_array[i].length=0;save_inode(i); \n"); for(i=0;i { if((inode_array[i].inum>0)&&(inode_array[i].type=='f')&&! strcmp(inode_array[i].file_name,argv[1])) { printf("Thisfileisexsit.\n"); return; } } for(i=0;i if(inode_array[i].inum<0)break; if(i==INODENUM) { printf("Inodeisfull.\n"); exit(-1); } inode_array[i].inum=i; strcpy(inode_array[i].file_name,argv[1]); inode_array[i].type='f';strcpy(inode_array[i].user_name,;inode_array[i].iparent=inum_cur;inode_array[i].length=0;save_inode(i); } \n"); return; } for(i=0;i if((inode_array[i].inum>0)&& (inode_array[i].type=='f')&& ! strcmp(inode_array[i].file_name,argv[1])&& ! strcmp(inode_array[i].user_name,) break; if(i==INODENUM) { doesn't printf("Thefileyouwanttoopen exsited.\n"); return; inum=i; printf("Pleaseinputopenmode: (1: read,2: write,3: readandwrite): "); scanf("%d",&mode); getchar(); if((mode<1)||(mode>3)) { printf("Openmodeiswrong.\n"); return; } for(i=0;i if(file_array[i].inum<0)break; if(i==FILENUM) { printf("Thefiletableisfull,pleaseclosesome file.\n"); return; } filenum=i; file_array[filenum].inum=inum;strcpy(file_array[filenum].file_name,inode_array[inum].file_name); file_array[filenum].mode=mode; printf("Openfile%sby",file_array[filenum].file_name); if(mode==1)printf("readonly.\n"); elseif(mode==2)printf("writeonly.\n"); elseprintf("readandwrite.\n"); } \n"); return; } for(i=0;i if((file_array[i].inum>0)&&! strcmp(file_array[i].file_name,argv[1]))break; if(i==FILENUM) { printf("Open%sfirst.\n",argv[1]); return; } elseif(file_array[i].mode==2) { printf("Can'tread%s.\n",argv[1]); return; inum=file_array[i].inum; printf("Thelengthof%s: %d.\n",argv[1],inode_array[inum].length); if(inode_array[inum].length>0) { read_blk(inum); for(i=0;(i ='\0');i++) printf("%c",temp[i]); } } \n"); return; } for(i=0;i if((file_array[i].inum>0)&&! strcmp(file_array[i].file_name,argv[1]))break; if(i==FILENUM) { printf("Open%sfirst.\n",argv[1]); return; elseif(file_array[i].mode==1) { printf("Can'twrite%s.\n",argv[1]); return; } inum=file_array[i].inum; %s: %d\n", printf("Thelengthof inode_array[inum].file_name,inode_array[inum].length); if(inode_array[inum].length==0) { i=0; inode_array[inum].address[0]=get_blknum();printf("Inputthedata(CTRL+Ztoend): \n");while(i<1023&&(temp[i]=getchar())! =EOF)i++;temp[i]='\0'; length=strlen(temp)+1; inode_array[inum].length=length; if(length>512)inode_array[inum].address[1]get_blknum(); save_inode(inum); write_blk(inum); } else printf("Thisfilecan'tbewritten.\n"); } \n"); return; } for(i=0;i argv[1])) ! strcmp(file_array[i].file_name,break; if(i==FILENUM) { printf("Thisfiledoesn'tbeopened.\n");return; } else { file_array[i].inum=-1; printf("Close%ssuccess! \n",argv[1]); num=-1; if(inode_array[i].length>0) { release_blk(inode_array[i].address[0]); if(inode_array[i].length>=512) release_blk(inode_array[i].address[1]); } save_inode(i); } \n"); return; } intn,t,i; stack for(i=0;i (inode_array[i].iparent==inum_cur)&& (! strcmp(inode_array[i].file_name,argv[1]))&& (! strcmp(inode_array[i].user_name,)) n=inode_array[i].inum; break; } if(i==INODENUM)puts("DirectoryERROR"); else { (n); while(! ()) { t=(); (); del(t); for(i=0;i >=0) if((inode_array[i].inum&&(inode_array[i].iparent==t)) (i); } } } //功能: 退出当前用户(logout) voidlogout() printf("Doyouwanttoexitthisuser(y/n)? "); scanf("%c",&choice); getchar(); if((choice=='y')||(choice=='Y')) { printf("\nCurrentuserhasexited! \n");login(); } return; } //功能: 退出文件系统(quit) voidquit() { printf("Doyouwanttoexist(y/n): "); scanf("%c",&choice); getchar(); if((choice=='y')||(choice=='Y')) exit(0); } //功能: 显示错误 voiderrcmd() printf("CommandError! ! ! \n"); } //清空内存中存在的用户名 voidfree_user() { inti; for(i=0;i<10;i++) [i]='\0'; } //功能: 循环执行用户输入的命令,直到logout //"help","cd","dir","mkdir","creat","open","read", "write","close","delete","logout","clear","format","quit" voidcommand(void) { charcmd[100]; system("cls"); do { pathset(); gets(cmd); switch(analyse(cmd)) help();break; case1: cd();break; case2: dir();break; case3: mkdir();break; case4: create();break; case5: open();break; case6: read();break; case7: write();break; case8: close();break; case9: delet();break;case10: logout();break; system("cls");break; case12: format(); init(); free_user(); login();break; case13: quit();break; case14: errcmd();break;default: break; } }while (1); } //主函数 intmain(void) { login(); init(); command(); return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 文件 管理 C+ 代码