操作系統(tǒng)實(shí)驗(yàn)文件管理C++代碼_第1頁(yè)
操作系統(tǒng)實(shí)驗(yàn)文件管理C++代碼_第2頁(yè)
操作系統(tǒng)實(shí)驗(yàn)文件管理C++代碼_第3頁(yè)
操作系統(tǒng)實(shí)驗(yàn)文件管理C++代碼_第4頁(yè)
操作系統(tǒng)實(shí)驗(yàn)文件管理C++代碼_第5頁(yè)
已閱讀5頁(yè),還剩14頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <string.h>#include<stack>using namespace std;#define BLKSIZE 512/ 數(shù)據(jù)塊的大小 #define BLKNUM 512/ 數(shù)據(jù)塊的塊數(shù)#define INODESIZE 32/ i節(jié)點(diǎn)的大小#define INODENUM 32/ i節(jié)點(diǎn)的數(shù)目#define FILENUM 8/ 打開(kāi)文件表的數(shù)目/用戶 typedef structchar us

2、er_name10; / 用戶名char password10; / 密碼 User;/i節(jié)點(diǎn) typedef structshort inum; / 文件i節(jié)點(diǎn)號(hào)char file_name10; / 文件名char type; / 文件類型char user_name10; / 文件所有者short iparent; / 父目錄的i節(jié)點(diǎn)號(hào) short length; / 文件長(zhǎng)度short address2; / 存放文件的地址 Inode;/打開(kāi)文件表 typedef structshort inum; / i節(jié)點(diǎn)號(hào)char file_name10; / 文件名short mode; /

3、 讀寫模式(1:read, 2:write, / 3:read and write) File_table;/ 申明函數(shù)void login(void);void init(void);int analyse(char *);void save_inode(int);int get_blknum(void);void read_blk(int);void write_blk(int);void release_blk(int);void pathset();void del(int);/ 用戶命令處理函數(shù) void help(void);void cd(void);void dir(void)

4、;void mkdir(void);void creat(void);void open(void);void read(void);void write(void);void close(void);void delet(void);void logout(void);void command(void);void quit();/main.cpp文件/#include "head.h"/定義全局變量 charchoice;intargc;/ 用戶命令的參數(shù)個(gè)數(shù)char*argv5;/ 用戶命令的參數(shù)intinum_cur;/ 當(dāng)前目錄chartemp2*BLKSIZE;

5、/ 緩沖區(qū)Useruser;/ 當(dāng)前的用戶charbitmapBLKNUM;/ 位圖數(shù)組Inodeinode_arrayINODENUM;/ i節(jié)點(diǎn)數(shù)組File_table file_arrayFILENUM;/ 打開(kāi)文件表數(shù)組charimage_name10 = "data.dat"/ 文件系統(tǒng)名稱FILE*fp;/ 打開(kāi)文件指針/創(chuàng)建映像hd,并將所有用戶和文件清除void format(void)int i;Inode inode;printf("Will be to format filesystem.n");printf("WARNI

6、NG: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("Can't create file %sn", image_nam

7、e); exit(-1); for(i = 0; i < BLKSIZE; 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.address0 = -1; inode.address1 = -1; fwrite(&inode, sizeof(Inode

8、), 1, fp); inode.inum = -1; for(i = 0; i < 31; i+) fwrite(&inode, sizeof(Inode), 1, fp); for(i = 0; i < BLKNUM*BLKSIZE; i+) fputc('0', fp); fclose(fp); / 打開(kāi)文件user.txt if(fp=fopen("user.txt", "w+") = NULL) printf("Can't create file %sn", "user.

9、txt"); exit(-1); fclose(fp); printf("Filesystem created successful.Please first login!n");return ;/ 功能: 用戶登陸,如果是新用戶則創(chuàng)建用戶void login(void)char *p;int flag;char user_name10;char password10;char file_name10 = "user.txt"do printf("login:"); gets(user_name); printf("

10、;password:"); p=password; while(*p=getch() if(*p = 0x0d) *p='0' /將輸入的回車鍵轉(zhuǎn)換成空格 break; printf("*"); /將輸入的密碼以"*"號(hào)顯示 p+; flag = 0; if(fp = fopen(file_name, "r+") = NULL) printf("nCan't open file %s.n", file_name); printf("This filesystem not

11、exist, it will be create!n"); format(); login(); while(!feof(fp) fread(&user, sizeof(User), 1, fp); / 已經(jīng)存在的用戶, 且密碼正確 if(!strcmp(user.user_name, user_name) && !strcmp(user.password, password) fclose(fp); printf("n"); return ; / 已經(jīng)存在的用戶, 但密碼錯(cuò)誤 else if(!strcmp(user.user_name,

12、 user_name) printf("nThis user is exist, but password is incorrect.n"); flag = 1; fclose(fp); break; if(flag = 0) break;while(flag);/ 創(chuàng)建新用戶if(flag = 0) printf("nDo you want to creat a new user?(y/n):"); scanf("%c", &choice); gets(temp); if(choice = 'y') | (c

13、hoice = '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節(jié)點(diǎn)讀入內(nèi)存void init(void)int i;if(fp = fopen(image_name, "r+b") = NULL) prin

14、tf("Can't open file %s.n", image_name); exit(-1);/ 讀入位圖for(i = 0; i < BLKNUM; i+) bitmapi = fgetc(fp);/ 顯示位圖/ 讀入i節(jié)點(diǎn)信息for(i = 0; i < INODENUM; i+) fread(&inode_arrayi, sizeof(Inode), 1, fp);/ 顯示i節(jié)點(diǎn)/ 當(dāng)前目錄為根目錄inum_cur = 0;/ 初始化打開(kāi)文件表for(i = 0; i < FILENUM; i+)file_arrayi.inum

15、= -1;/ 功能: 分析用戶命令, 將分析結(jié)果填充argc和argv/ 結(jié)果: 0-13為系統(tǒng)命令, 14為命令錯(cuò)誤int analyse(char *str)int i;char temp20;char *ptr_char;char *syscmd="help", "cd", "dir", "mkdir", "create", "open", "read", "write", "close", "del

16、et", "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') tempi+ = *ptr_char+; argvargc = (char *)malloc(

17、i+1); strncpy(argvargc, temp, i); argvargci = '0' argc+; i = 0; if(*ptr_char = '0') break; if(argc != 0) for(i = 0; (i < 14) && strcmp(argv0, syscmdi); i+); return i;else return 14;/ 功能: 將num號(hào)i節(jié)點(diǎn)保存到hd.datvoid save_inode(int num)if(fp=fopen(image_name, "r+b") = NU

18、LL) printf("Can't open file %sn", image_name); exit(-1);fseek(fp, BLKNUM +num*sizeof(Inode), SEEK_SET);fwrite(&inode_arraynum, sizeof(Inode), 1, fp);fclose(fp);/ 功能: 申請(qǐng)一個(gè)數(shù)據(jù)塊int get_blknum(void)int i;for(i = 0; i < BLKNUM; i+) if(bitmapi = '0') break;/ 未找到空閑數(shù)據(jù)塊if(i = BLKN

19、UM) printf("Data area is full.n"); exit(-1);bitmapi = '1'if(fp=fopen(image_name, "r+b") = NULL) printf("Can't open file %sn", image_name); exit(-1);fseek(fp, i, SEEK_SET);fputc('1', fp);fclose(fp);return i;/ 功能: 將i節(jié)點(diǎn)號(hào)為num的文件讀入temp void read_blk(int n

20、um)int i, len;char ch;int add0, add1;len = inode_arraynum.length;add0 = inode_arraynum.address0;if(len > 512) add1 = inode_arraynum.address1;if(fp = fopen(image_name, "r+b") = NULL) printf("Can't open file %s.n", image_name); exit(-1);fseek(fp, BLKSIZE+INODESIZE*INODENUM +

21、add0*BLKSIZE, SEEK_SET);ch = fgetc(fp);for(i=0; (i < len) && (ch != '0') && (i < 512); i+) tempi = ch; ch = fgetc(fp);if(i >= 512) fseek(fp,BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); ch = fgetc(fp); for(; (i < len) && (ch != '0'); i+) temp

22、i = ch; ch = fgetc(fp); tempi = '0'fclose(fp);/ 功能: 將temp的內(nèi)容輸入hd的數(shù)據(jù)區(qū)void write_blk(int num)int i, len;int add0, add1;add0 = inode_arraynum.address0;len = inode_arraynum.length;if(fp = fopen(image_name, "r+b") = NULL) printf("Can't open file %s.n", image_name); exit(-1

23、);fseek(fp, BLKSIZE+INODESIZE*INODENUM+add0*BLKSIZE, SEEK_SET);for(i=0; (i<len)&&(tempi!='0')&&(i < 512); i+) fputc(tempi, fp);if(i = 512) add1 = inode_arraynum.address1; fseek(fp, BLKSIZE+INODESIZE*INODENUM+add1*BLKSIZE, SEEK_SET); for(; (i < len) && (tempi

24、!= '0'); i+) fputc(tempi, fp);fputc('0', fp);fclose(fp);/ 功能: 釋放文件塊號(hào)為num的文件占用的空間void release_blk(int num)FILE *fp;if(fp=fopen(image_name, "r+b") = NULL) printf("Can't open file %sn", image_name); exit(-1);bitmapnum = '0'fseek(fp, num, SEEK_SET);fputc(&

25、#39;0', fp);fclose(fp);/ 功能: 顯示幫助命令void help(void)printf("command: n help - show help menu nclear - clear the screen ncd - change directory nmkdir - make directory ncreate - create a new file nopen - open a exist file nread - read a file nwrite - write something to a file nclose - close a f

26、ile ndelet - delete a exist file or directory nformat - format a exist filesystem nlogout - exit user nquit - exit this systemn");/設(shè)置文件路徑void pathset()char path50;int m,n;if(inode_arrayinum_cur.inum = 0) strcpy(path,user.user_name);else strcpy(path,user.user_name); m=0; n=inum_cur; while(m != i

27、num_cur) while(inode_arrayn.iparent != m) n = inode_arrayn.iparent; strcat(path,"/"); strcat(path,inode_arrayn.file_name); m = n; n = inum_cur; printf("%s$",path);/ 功能: 切換目錄(cd . 或者 cd dir1)void cd(void)int i;if(argc != 2) printf("Command cd must have two args. n"); ret

28、urn ;if(!strcmp(argv1, ".") inum_cur = inode_arrayinum_cur.iparent;else for(i = 0; i < INODENUM; i+) if(inode_arrayi.inum>0)&& (inode_arrayi.type='d')&& (inode_arrayi.iparent=inum_cur)&& !strcmp(inode_arrayi.file_name,argv1)&& !strcmp(inode_arr

29、ayi.user_name,user.user_name) break; if(i = INODENUM) printf("This directory isn't exsited.n"); else inum_cur = i;/ 功能: 顯示當(dāng)前目錄下的子目錄和文件(dir)void dir(void)int i;int dcount=0,fcount=0;short bcount=0;if(argc != 1) printf("Command dir must have one args. n"); return ;/ 遍歷i節(jié)點(diǎn)數(shù)組, 顯示

30、當(dāng)前目錄下的子目錄和文件名for(i = 0; i < INODENUM; i+) if(inode_arrayi.inum> 0) && (inode_arrayi.iparent = inum_cur)&& !strcmp(inode_arrayi.user_name,user.user_name) if(inode_arrayi.type = 'd') dcount+; printf("%-20s<DIR>n", inode_arrayi.file_name); else fcount+; bco

31、unt+=inode_arrayi.length; printf("%-20s%12d bytesn", inode_arrayi.file_name,inode_arrayi.length); printf("n %d file(s)%11d bytesn",fcount,bcount);printf(" %d dir(s) %11d bytes FreeSpacen",dcount,1024*1024-bcount);/ 功能: 在當(dāng)前目錄下創(chuàng)建子目錄(mkdir dir1)void mkdir(void)int i;if(arg

32、c != 2)printf("command mkdir must have two args. n");return ;/ 遍歷i節(jié)點(diǎn)數(shù)組, 查找未用的i節(jié)點(diǎn)for(i = 0; i < INODENUM; i+) 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 =

33、 'd'strcpy(inode_arrayi.user_name,user.user_name);inode_arrayi.iparent = inum_cur;inode_arrayi.length = 0;save_inode(i);/ 功能: 在當(dāng)前目錄下創(chuàng)建文件(creat file)void create(void)int i;if(argc != 2) printf("command creat must have two args. n"); return ;for(i = 0; i < INODENUM; i+) if(inode_a

34、rrayi.inum > 0) && (inode_arrayi.type = 'f') && !strcmp(inode_arrayi.file_name, argv1) printf("This file is exsit.n"); return ; for(i = 0; i < INODENUM; i+) if(inode_arrayi.inum < 0) break;if(i = INODENUM) printf("Inode is full.n"); exit(-1);inode

35、_arrayi.inum = i;strcpy(inode_arrayi.file_name, argv1);inode_arrayi.type = 'f'strcpy(inode_arrayi.user_name, user.user_name);inode_arrayi.iparent = inum_cur;inode_arrayi.length = 0;save_inode(i);/ 功能: 打開(kāi)當(dāng)前目錄下的文件(open file1)void open()int i, inum, mode, filenum;if(argc != 2) printf("comm

36、and open must have two args. n"); return ;for(i = 0; i < INODENUM; i+) if(inode_arrayi.inum > 0) && (inode_arrayi.type = 'f') && !strcmp(inode_arrayi.file_name,argv1)&& !strcmp(inode_arrayi.user_name,user.user_name) break;if(i = INODENUM) printf("The f

37、ile you want to open doesn't exsited.n"); return ;inum = i;printf("Please input open mode:(1: read, 2: write, 3: read and write):");scanf("%d", &mode);getchar();if(mode < 1) | (mode > 3) printf("Open mode is wrong.n"); return;for(i = 0; i < FILENUM

38、; i+) if(file_arrayi.inum < 0) break;if(i = FILENUM) printf("The file table is full, please close some file.n"); return ;filenum = i;file_arrayfilenum.inum = inum; strcpy(file_arrayfilenum.file_name, inode_arrayinum.file_name);file_arrayfilenum.mode = mode; printf("Open file %s by

39、", 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");/ 功能: 從文件中讀出字符(read file1)void read()int i, inum;if(argc != 2) printf("command read must have two args. n"); return

40、;for(i = 0; i < FILENUM; i+) if(file_arrayi.inum > 0) && !strcmp(file_arrayi.file_name,argv1) break;if(i = FILENUM) printf("Open %s first.n", argv1); return ;else if(file_arrayi.mode = 2) printf("Can't read %s.n", argv1); return ;inum = file_arrayi.inum;printf(&

41、quot;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);/ 功能: 向文件中寫入字符(write file1)void write()int i, inum, length;if(argc != 2)

42、printf("Command write must have two args. n"); return ;for(i = 0; i < FILENUM; i+) if(file_arrayi.inum>0)&& !strcmp(file_arrayi.file_name,argv1) break; if(i = FILENUM) printf("Open %s first.n", argv1); return ;else if(file_arrayi.mode = 1) printf("Can't wri

43、te %s.n", argv1); return ;inum = file_arrayi.inum;printf("The length of %s:%dn", inode_arrayinum.file_name, inode_arrayinum.length);if(inode_arrayinum.length = 0) i=0; inode_arrayinum.address0 = get_blknum(); printf("Input the data(CTRL+Z to end):n"); while(i<1023&&am

44、p;(tempi=getchar()!=EOF) i+; tempi='0' length=strlen(temp)+1; inode_arrayinum.length=length; if(length > 512) inode_arrayinum.address1 = get_blknum(); save_inode(inum); write_blk(inum);else printf("This file can't be written.n");/ 功能: 關(guān)閉已經(jīng)打開(kāi)的文件(close file1)void close(void)in

45、t i;if(argc != 2) printf("Command close must have two args. n"); return ;for(i = 0; i < FILENUM; i+) if(file_arrayi.inum > 0) && !strcmp(file_arrayi.file_name, argv1) break;if(i = FILENUM) printf("This file doesn't be opened.n"); return ;else file_arrayi.inum =

46、-1; printf("Close %s success!n", argv1);/回收i節(jié)點(diǎn),有文件則刪除文件void del(int i)inode_arrayi.inum = -1;if(inode_arrayi.length > 0) release_blk(inode_arrayi.address0); if(inode_arrayi.length >= 512) release_blk(inode_arrayi.address1);save_inode(i);/刪除子目錄樹(shù)和文件void delet(void) if(argc != 2) printf

47、("Command delete must have two args. n"); return ;int n,t,i;stack<int> istk; for(i = 0; i < INODENUM; i+)/查找待刪除子目錄 if(inode_arrayi.inum >=0) && (inode_arrayi.iparent = inum_cur)&& (!strcmp(inode_arrayi.file_name,argv1)&& (!strcmp(inode_arrayi.user_name,user.user_name) n=inod

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論