您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > 操作系统实验文件管理C++代码
#includestdio.h#includestdlib.h#includeconio.h#includestring.h#includestackusingnamespacestd;#defineBLKSIZE512//数据块的大小#defineBLKNUM512//数据块的块数#defineINODESIZE32//i节点的大小#defineINODENUM32//i节点的数目#defineFILENUM8//打开文件表的数目//用户typedefstruct{charuser_name[10];//用户名charpassword[10];//密码}User;//i节点typedefstruct{shortinum;//文件i节点号charfile_name[10];//文件名chartype;//文件类型charuser_name[10];//文件所有者shortiparent;//父目录的i节点号shortlength;//文件长度shortaddress[2];//存放文件的地址}Inode;//打开文件表typedefstruct{shortinum;//i节点号charfile_name[10];//文件名shortmode;//读写模式(1:read,2:write,//3:readandwrite)}File_table;//申明函数voidlogin(void);voidinit(void);intanalyse(char*);voidsave_inode(int);intget_blknum(void);voidread_blk(int);voidwrite_blk(int);voidrelease_blk(int);voidpathset();voiddel(int);//用户命令处理函数voidhelp(void);voidcd(void);voiddir(void);voidmkdir(void);voidcreat(void);voidopen(void);voidread(void);voidwrite(void);voidclose(void);voiddelet(void);voidlogout(void);voidcommand(void);voidquit();//main.cpp文件//#includehead.h//定义全局变量charchoice;intargc;//用户命令的参数个数char*argv[5];//用户命令的参数intinum_cur;//当前目录chartemp[2*BLKSIZE];//缓冲区Useruser;//当前的用户charbitmap[BLKNUM];//位图数组Inodeinode_array[INODENUM];//i节点数组File_tablefile_array[FILENUM];//打开文件表数组charimage_name[10]=data.dat;//文件系统名称FILE*fp;//打开文件指针//创建映像hd,并将所有用户和文件清除voidformat(void){inti;Inodeinode;printf(Willbetoformatfilesystem...\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;iBLKSIZE;i++)fputc('0',fp);inode.inum=0;strcpy(inode.file_name,/);inode.type='d';strcpy(inode.user_name,/);inode.iparent=0;inode.length=0;inode.address[0]=-1;inode.address[1]=-1;fwrite(&inode,sizeof(Inode),1,fp);inode.inum=-1;for(i=0;i31;i++)fwrite(&inode,sizeof(Inode),1,fp);for(i=0;iBLKNUM*BLKSIZE;i++)fputc('\0',fp);fclose(fp);//打开文件user.txtif((fp=fopen(user.txt,w+))==NULL){printf(Can'tcreatefile%s\n,user.txt);exit(-1);}fclose(fp);printf(Filesystemcreatedsuccessful.Pleasefirstlogin!\n);}return;}//功能:用户登陆,如果是新用户则创建用户voidlogin(void){char*p;intflag;charuser_name[10];charpassword[10];charfile_name[10]=user.txt;do{printf(login:);gets(user_name);printf(password:);p=password;while(*p=getch()){if(*p==0x0d){*p='\0';//将输入的回车键转换成空格break;}printf(*);//将输入的密码以*号显示p++;}flag=0;if((fp=fopen(file_name,r+))==NULL){printf(\nCan'topenfile%s.\n,file_name);printf(Thisfilesystemnotexist,itwillbecreate!\n);format();login();}while(!feof(fp)){fread(&user,sizeof(User),1,fp);//已经存在的用户,且密码正确if(!strcmp(user.user_name,user_name)&&!strcmp(user.password,password)){fclose(fp);printf(\n);return;}//已经存在的用户,但密码错误elseif(!strcmp(user.user_name,user_name)){printf(\nThisuserisexist,butpasswordisincorrect.\n);flag=1;fclose(fp);break;}}if(flag==0)break;}while(flag);//创建新用户if(flag==0){printf(\nDoyouwanttocreatanewuser?(y/n):);scanf(%c,&choice);gets(temp);if((choice=='y')||(choice=='Y')){strcpy(user.user_name,user_name);strcpy(user.password,password);fwrite(&user,sizeof(User),1,fp);fclose(fp);return;}if((choice=='n')||(choice=='N'))login();}}//功能:将所有i节点读入内存voidinit(void){inti;if((fp=fopen(image_name,r+b))==NULL){printf(Can'topenfile%s.\n,image_name);exit(-1);}//读入位图for(i=0;iBLKNUM;i++)bitmap[i]=fgetc(fp);//显示位图//读入i节点信息for(i=0;iINODENUM;i++)fread(&inode_array[i],sizeof(Inode),1,fp);//显示i节点//当前目录为根目录inum_cur=0;//初始化打开文件表for(i=0;iFILENUM;i++)file_array[i].inum=-1;}//功能:分析用户命令,将分析结果填充argc和argv//结果:0-13为系统命令,14为命令错误intanalyse(char*str){inti;chartemp[20];char*ptr_char;char*syscmd[]={help,cd,dir,mkdir,create,open,read,write,close,delet,logout,clear,format,quit};argc=0;for(i=0,ptr_char=str;*ptr_char!='\0';ptr_char++){if(*ptr_char!=''){while(*ptr_char!=''&&(*ptr_char!='\0'))temp[i++]=*ptr_char++;argv[argc]=(char*)malloc(i+1);strncpy(argv[argc],temp,i);argv[argc][i]='\0';argc++;i=0;if(*ptr_char=='\0')break;}}if(argc!=0){for(i=0;(i14)&&strcmp(argv[0],syscmd[i]);i++);returni;}elsereturn14;}//功能:将num号i节点保存到hd.datvoidsave_inode(intnum){if((fp=fopen(image_name,r+b))==NULL){printf(Can'topenfile%s\n,image_name);exit(-1);}fseek(fp,BLKNUM+num*sizeof(Inode),SEEK_SET);fwrite(&inode_array[num],sizeof(Inode),1,fp);fclose(fp);}//功能:申请一个数据块intget_blknum(void){inti;for(i=0;iBLKNUM;i++)if(bitmap[i]=='0')break;//未找到空闲数据块if(i==BLKNUM){printf(Dataareaisfull.\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;}//功能:将i节点号为num的文件读入tempvoidread_blk(intnum){inti,len;charch;intadd0,add1;len=inode_array[num].length;add0=inode_array[num].address[0];if(len512)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;(ilen)&&(ch!='\0')&&(i512);i++){temp[i]=ch;ch=fgetc(fp);}if(i=512){fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE,SEEK_SET);ch=fgetc(fp);
本文标题:操作系统实验文件管理C++代码
链接地址:https://www.777doc.com/doc-1992843 .html