1、操作系统实验文件管理C+代码#include #include #include #include #includeusing namespace std;#define BLKSIZE 512 .n);printf(WARNING:ALL DATA ON THIS FILESYSTEM WILL BE LOST!n);printf(Proceed with Format(Y/N)?);scanf(%c, &choice);getchar();if(choice = y) | (choice = Y)if(fp=fopen(image_name, w+b) = NULL)printf(Cant
2、 create file %sn, image_name);exit(-1);for(i = 0; i BLKSIZE; 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 512)add1 = inode_arraynum.address1;if(fp = fopen(image_name, r
3、+b) = NULL)printf(Cant open file %s.n, image_name);exit(-1);fseek(fp, BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE, SEEK_SET);ch = fgetc(fp);for(i=0; (i len) & (ch != 0) & (i = 512) fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE,SEEK_SET);ch = fgetc(fp);for(; (i len) & (ch != 0); i+)tempi = ch;ch = fge
4、tc(fp);tempi = 0;fclose(fp);ddress0;len = inode_arraynum.length;if(fp = fopen(image_name, r+b) = NULL)printf(Cant open file %s.n, image_name);exit(-1);fseek(fp, BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE, SEEK_SET);for(i=0; (ilen)&(tempi!=0)&(i 512); i+)fputc(tempi, fp);if(i = 512)add1 = inode_arraynum
5、.address1;fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET);for(; (i len) & (tempi != 0); i+)fputc(tempi, fp);fputc(0, fp);fclose(fp);num = 0)strcpy(path,;elsestrcpy(path,;m=0;n=inum_cur;while(m != inum_cur)while(inode_arrayn.iparent != m)n = inode_arrayn.iparent;strcat(path,/);strcat(path
6、,inode_arrayn.file_name); m = n;n = inum_cur;printf(%s$,path);或者 cd dir1)void cd(void)int i;if(argc != 2)printf(Command cd must have two args. n);if(!strcmp(argv1, .)inum_cur = inode_arrayinum_cur.iparent; elsefor(i = 0; i 0)& (inode_arrayi.type=d)&(inode_arrayi.iparent=inum_cur)&!strcmp(inode_array
7、i.file_name, argv1)&!strcmp(inode_arrayi.user_name ,)break;if(i = INODENUM)printf(This directory isnt exsited.n); elseinum_cur = i;n);num 0) &(inode_arrayi.iparent = inum_cur)&!strcmp(inode_arrayi.user_name,)if(inode_arrayi.type = d)dcount+;printf(%-20sn,inode_arrayi.file_name);elsefcount+;bcount+=i
8、node_arrayi.length;bytesn,printf(%-20s%12dinode_arrayi.file_name,inode_arrayi.length);printf(n%d file(s)%11dbytesn,fcount,bcount);%11d bytesprintf( %d dir(s) FreeSpacen,dcount,1024*1024-bcount);n);return ;num 0) break;if(i = INODENUM)printf(Inode is full.n);exit(-1);inode_arrayi.inum = i;strcpy(inod
9、e_arrayi.file_name, argv1); inode_arrayi.type = d;strcpy(inode_arrayi.user_name,;inode_arrayi.iparent = inum_cur;inode_arrayi.length = 0; save_inode(i);n);for(i = 0; i 0) & (inode_arrayi.type = f) & !strcmp(inode_arrayi.file_name, argv1)printf(This file is exsit.n);return ;for(i = 0; i INODENUM; i+)
10、if(inode_arrayi.inum 0) break;if(i = INODENUM)printf(Inode is full.n);exit(-1);inode_arrayi.inum = i;strcpy(inode_arrayi.file_name, argv1);inode_arrayi.type = f; strcpy(inode_arrayi.user_name, ; inode_arrayi.iparent = inum_cur; inode_arrayi.length = 0; save_inode(i);n);return ;for(i = 0; i 0) &(inod
11、e_arrayi.type = f) &!strcmp(inode_arrayi.file_name,argv1)&!strcmp(inode_arrayi.user_name,)break;if(i = INODENUM)doesntprintf(The file you want to openexsited.n);return ;inum = i;printf(Please input open mode:(1: read, 2: write, 3: read and write):);scanf(%d, &mode);getchar();if(mode 3)printf(Open mo
12、de is wrong.n);return;for(i = 0; i FILENUM; i+)if(file_arrayi.inum 0) break;if(i = FILENUM)printf(The file table is full, please close somefile.n);return ;filenum = i;file_arrayfilenum.inum = inum; strcpy(file_arrayfilenum.file_name, inode_arrayinum.file_name);file_arrayfilenum.mode = mode;printf(Op
13、en file %sby , file_arrayfilenum.file_name);if(mode = 1) printf(read only.n);else if(mode = 2) printf(write only.n);else printf(read and write.n);n);return;for(i = 0; i 0) & !strcmp(file_arrayi.file_name,argv1) break;if(i = FILENUM)printf(Open %s first.n, argv1);return ;else if(file_arrayi.mode = 2)
14、printf(Cant read %s.n, argv1);return ;inum = file_arrayi.inum;printf(The length of %s:%d.n, argv1, inode_arrayinum.length);if(inode_arrayinum.length 0)read_blk(inum);for(i = 0; (i inode_arrayinum.length) & (tempi != 0); i+)printf(%c, tempi);n);return ;for(i = 0; i 0)& !strcmp(file_arrayi.file_name,a
15、rgv1) break;if(i = FILENUM)printf(Open %s first.n, argv1);return ;else if(file_arrayi.mode = 1)printf(Cant write %s.n, argv1);return ;inum = file_arrayi.inum;%s:%dn,printf(The length ofinode_arrayinum.file_name, inode_arrayinum.length);if(inode_arrayinum.length = 0)i=0;inode_arrayinum.address0 = get
16、_blknum(); printf(Input the data(CTRL+Z to end):n); while(i 512) inode_arrayinum.address1 get_blknum();save_inode(inum);write_blk(inum);elseprintf(This file cant be written.n);n);return ;for(i = 0; i 0) &argv1)!strcmp(file_arrayi.file_name, break;if(i = FILENUM)printf(This file doesnt be opened.n);
17、return ;elsefile_arrayi.inum = -1;printf(Close %s success!n, argv1);num = -1;if(inode_arrayi.length 0)release_blk(inode_arrayi.address0);if(inode_arrayi.length = 512)release_blk(inode_arrayi.address1);save_inode(i);n);return ;int n,t,i;stack istk;for(i = 0; i =0) &(inode_arrayi.iparent = inum_cur)&(
18、!strcmp(inode_arrayi.file_name,argv1)&(!strcmp(inode_arrayi.user_name,)n=inode_arrayi.inum;break;if(i=INODENUM) puts(Directory ERROR);else(n);while(!()t=();();del(t);for(i = 0; i =0)if(inode_arrayi.inum &(inode_arrayi.iparent = t)(i);/ 功能 : 退出当前用户 (logout)void logout()printf(Do you want to exit this
19、 user(y/n)?);scanf(%c, &choice);getchar();if(choice = y) | (choice = Y)printf(nCurrent user has exited!n); login();return ;/ 功能 : 退出文件系统 (quit)void quit()printf(Do you want to exist(y/n):);scanf(%c, &choice);getchar();if(choice = y) | (choice = Y)exit(0);/ 功能 : 显示错误void errcmd()printf(Command Error!
20、n);/ 清空内存中存在的用户名void free_user()int i;for(i=0;i10;i+)i=0;/ 功能 : 循环执行用户输入的命令 , 直到 logout/ help, cd, dir, mkdir, creat, open,read,write, close, delete, logout, clear, format,quitvoid command(void)char cmd100;system(cls);dopathset();gets(cmd);switch(analyse(cmd)help(); break;case 1:cd(); break;case 2:d
21、ir(); break;case 3:mkdir(); break;case 4:create(); break;case 5:open(); break;case 6:read(); break;case 7:write(); break;case 8:close(); break;case 9:delet(); break; case 10:logout();break;system(cls);break;case 12:format();init();free_user();login();break;case 13:quit(); break;case 14:errcmd(); break; default:break;while(1);/ 主函数int main(void)login();init();command();return 0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1