文件管理系統(tǒng)試驗報告_第1頁
文件管理系統(tǒng)試驗報告_第2頁
文件管理系統(tǒng)試驗報告_第3頁
文件管理系統(tǒng)試驗報告_第4頁
文件管理系統(tǒng)試驗報告_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗 成績姓名任倩倩倪晨學(xué)號20112105902011210542華中師范大學(xué)計算機科學(xué)系實驗報告書實驗題目:基于兩級目錄結(jié)構(gòu)的簡單文件系統(tǒng)的管理與操作課程名稱:操作系統(tǒng)主講教師:輔導(dǎo)教師:葉俊民教授葉俊民教授班級:1班實驗時間:2013 年12月10日精選資料,歡迎下載目錄CDI0性質(zhì)的實踐步驟: 2構(gòu)思性實踐(C)(做什么) 2設(shè)計性實踐(D)(準備怎么做) 2實現(xiàn)性實踐(1)(做的結(jié)果) 2運行性實踐(0)(做的方式) 3一、實驗?zāi)康?3二、實驗內(nèi)容 3三、實驗環(huán)境 3四、實驗詳細實現(xiàn)過程與算法流程 3五、源程序(加注釋) 5六、實驗結(jié)果 19七、實驗總結(jié) 21CDIO性質(zhì)的實踐步驟:

2、構(gòu)思性實踐(C)采用兩級目錄,即設(shè)置主文件目錄MFD和用戶文件目錄UED。在主文件目 錄中,每個用戶目錄都占有一個目錄項,其目錄項中包含文件名和指向該文件目 錄文件。在用戶文件目錄UFD中,這些文件目錄具有相似的結(jié)構(gòu)。 具體測試時可 考慮:設(shè)計一個10個用戶的文件系統(tǒng),每次用戶可保存10個文件,一次運行用 戶可以打開5個文件,并需要對文件設(shè)置保護措施。(2)設(shè)計并實現(xiàn) Create、Open Read Write、Close、Delete 等文件命令的 算法,用戶可通過菜單方式或者 API方式實現(xiàn)對自己文件的管理。設(shè)計性實踐(D)1、 確定基本數(shù)據(jù)結(jié)構(gòu), MFD UFD.,Userinf,F(xiàn)i

3、leinf ,F(xiàn)ile,以及每個數(shù) 據(jù)結(jié)構(gòu)可以進行的對應(yīng)操作。2、確定每個函數(shù)的參數(shù)、返回對象,并且完成每個函數(shù),沒完成一個函數(shù), 需要進行一次測試。3、確定用戶菜單,并將用戶菜單與對應(yīng)的函數(shù)操作進行關(guān)聯(lián)。4、完整測試每個功能,找出存在的缺陷,并對用戶界面做適當調(diào)整。具體可參見本實驗報告的“三、實驗環(huán)境”和“四、實驗設(shè)計原理”。實現(xiàn)性實踐(I)1、完成代碼編寫,形成.c源文件與.exe可執(zhí)行文件;2、進行演示,演示應(yīng)包含所有功能點,將演示結(jié)果截圖保存;完成實驗報告說明,具體可參見本實驗報告的“五、實驗詳細實現(xiàn)過程與算 法流程”、“六、實驗調(diào)試”和“七、源程序”。運行性實踐(O)(1)小組成員確

4、定程序需要完成的功能,并詳細列出功能點為后期檢查程序是否滿足需求做準備。(2)確定編碼人員,由編碼人員完成基本編碼工作,另一人負責對完成的程序 進行測試,檢查是否滿足所有的功能點。(3)對不滿足的功能點或缺少的功能點進行修改或增加,重復(fù)第2步,直至符合要求。(4)完成上機驗收。具體可參見本實驗報告的和“九、實驗結(jié)果分析”和“十一、實驗總結(jié)”。(5) 實驗?zāi)康模簽榱四M文件系統(tǒng)實現(xiàn)的基本功能,了解文件系統(tǒng)的基本結(jié)構(gòu)和文件的各種 管理方法,加深理解文件系統(tǒng)的內(nèi)部功能及內(nèi)部實現(xiàn), 理解文件管理的工作過程, 加深各種文件操作命令的實質(zhì)內(nèi)容和執(zhí)行過程的了解, 現(xiàn)要求編程模擬一個簡單 的文件系統(tǒng),以實現(xiàn)對

5、文件系統(tǒng)的管理和控制功能, 支持用戶對自己文件的管理 與操作。二、實驗內(nèi)容:(6)要求本文件系統(tǒng)采用兩級目錄,即設(shè)置主文件目錄MFD和用戶文件目錄UED。在主文件目錄中,每個用戶目錄都占有一個目錄項,其目錄項中 包含文件名和指向該文件目錄文件。 在用戶文件目錄UFD中,這些文件目 錄具有相似的結(jié)構(gòu)。具體測試時可考慮:設(shè)計一個10個用戶的文件系統(tǒng), 每次用戶可保存10個文件,一次運行用戶可以打開 5個文件,并需要對 文件設(shè)置保護措施。(2)設(shè)計并實現(xiàn) Create、Open Read Write、Close、Delete 等文件命令的 算法,用戶可通過菜單方式或者 API方式實現(xiàn)對自己文件的管理

6、。三、實驗環(huán)境:Windows操作系統(tǒng)、VC+6.0 C語言(7)實驗詳細實現(xiàn)過程與算法流程my_format :對文件存儲器進行格式化,即按照文件系統(tǒng)的結(jié)構(gòu)對虛擬磁盤空間進行布局, 并在其上創(chuàng)建根目錄以及用于管理文件存儲空間等的數(shù)據(jù)結(jié)構(gòu)。my_mkdir :用于創(chuàng)建子目錄。my_rmdir :用于刪除子目錄。my_ls :用于顯示目錄中的內(nèi)容。my_cd:用于更改當前目錄。my_create :用于創(chuàng)建文件。my_ope n:用于打開文件。my_close :用于關(guān)閉文件。my_write :用于寫文件。my_read:用于讀文件。my_rm用于刪除文件。my_exitsys :用于退出文件

7、系統(tǒng)。1打開文件函數(shù)fope n()(1) 格式: FILE *fope n(const char *file name,c onst char *mode)(2) 功能:按照指定打開方式打開指定文件。(3 )輸入?yún)?shù)說明:filename :待打開的文件名,如果不存在就創(chuàng)建該文件。mode:文件打開方式,常用的有:"r":為讀而打開文本文件(不存在則出錯)。"w":為寫而打開文本文件(若不存在則創(chuàng)建該文件;反之,則從文件起始位置寫,原內(nèi)容將被覆蓋)。"a":為在文件末尾添加數(shù)據(jù)而打開文本文件。(若不存在則創(chuàng)建該文件;反之,在原文件末

8、尾追加)。"r+":為讀和寫而打開文本文件。(讀時,從頭開始;在寫數(shù)據(jù)時,新數(shù)據(jù)只覆蓋所占的空間, 其后不變)。"w+":首先建立一個新文件,進行寫操作,隨后可以從頭開始讀。(若文件存在,原內(nèi)容將全部消失)。"a+":功能與"a"相同;只是在文件末尾添加新的數(shù)據(jù)后,可以從頭開始讀。另外,上述模式字符串中都可以加一個"b”字符,如rb、wb ab、rb+、wb+、ab+等組合,字符"b”表示fopen()函數(shù)打開的文件為二進制文件,而非純文字文件。(4)輸出:一個指向FILE類型的指針。2. 關(guān)閉

9、文件函數(shù)fclose()(1) 格式:int fclose(FILE * stream);(2) 功能:用來關(guān)閉先前 fopen()打開的一個文件。此動作會讓緩沖區(qū)內(nèi)的數(shù)據(jù)寫入文件 中,并釋放系統(tǒng)所提供的文件資源。(3 )輸入?yún)?shù)說明:stream :指向要關(guān)閉文件的指針,它是先前執(zhí)行fopen()函數(shù)的返回值。(4)輸出:若關(guān)閉文件成功則返回0;有錯誤發(fā)生時則返回EOF并把錯誤代碼存到 errno。3. 讀文件函數(shù)fread()(1) 格式: size_t fread( void *buffer, size_t size, size_t count, FILE *stream );(2 )功

10、能:讀二進制文件到內(nèi)存。(3 )輸入?yún)?shù)說明:buffer :用于存放輸入數(shù)據(jù)的緩沖區(qū)的首地址;stream :使用fopen()打開的文件的指針,用于指示要讀取的文件;size :每個數(shù)據(jù)塊的字節(jié)數(shù);count :要讀入的數(shù)據(jù)塊的個數(shù);size*cou nt :表示要求讀取的字節(jié)數(shù)。(4)輸出:實際讀取的數(shù)據(jù)塊的個數(shù)。4. 寫文件函數(shù)fwrite()(1) 格式:size_t fwite(constvoid *buffer,size_t size,size_t count,FILE *stream);(2) 功能:將數(shù)據(jù)寫到二進制文件中。(3 )輸入?yún)?shù)說明:buffer :用于存放輸出數(shù)據(jù)

11、的緩沖區(qū)的首地址;stream :使用fopen()打開的文件的指針,用于指示要寫出的文件;size :每個數(shù)據(jù)塊的字節(jié)數(shù);count :要寫出的數(shù)據(jù)塊的個數(shù);size*cou nt :表示要求寫出的字符數(shù)。(4)輸出:實際寫出的數(shù)據(jù)塊的個數(shù)。5.判斷文件結(jié)束函數(shù)feof ()(1) 格式:int feof(FILE * stream)(2) 功能:用來判斷是否已讀取到文件末尾。(3 )輸入?yún)?shù)說明:stream :使用fopen()打開的文件的指針,用于指示要判斷的文件。(4)輸出:如果已讀到文件尾則返回非零值,其他情況返回0。6主要函數(shù)功能實現(xiàn):int format。; /格式化磁盤int

12、 mkdir(char *son fname); /倉 U建子目錄int rmdir(char *son fname); /刪除子目錄int create(char *n ame); /倉 U建文件in t listshow();顯示子文件信息int delfile(char *n ame); /刪除文件in t cha ngePath(char *so nfname);/更改當前路徑int write(char *n ame); /寫入文件int exit();/ 退出系統(tǒng)in t ope n( char *file);/打開文件in t close(char *file);/關(guān)閉文件int

13、 read(char *file);/讀取文件五、源程序:#i nclude <stdio.h>#in clude <memory.h>#in clude <stri ng>#in clude <iostream>using n amespace std;#define GENERAL 1/1 代表普通文件2代表目錄文件 0表示空文件#defi ne DIRECTORY 2#defi ne Zero 0 struct FCB文件名空文件文件大小char fname16; /目錄文件2空文件當前的父目錄盤塊號當前的盤塊char type; / 0i

14、nt size; /int fatherBlockNum; / int curre ntBlockNum; /void in itialize()strcpy(fname,"O");type = Zero;size =0; fatherBlockNum = curre ntBlockNum = 0;常量設(shè)置*/盤塊大小能打開最多的文件數(shù)盤塊數(shù)con st char* FilePath = "C:myfiles"/* con st int BlockSize = 512;/const int OPEN_MAX = 5;/const int BlockCou

15、 nt = 128; /磁盤大小目錄文件的最多 FCB數(shù)con st int DiskSize = BlockSize * BlockCou nt; / con st int BlockFcbCou nt = BlockSize/sizeof(FCB);int Ope nFileCou nt = 0; /統(tǒng)計當前打開文件數(shù)目struct OPENLIST /用戶文件打開表int files; /當前打開文件數(shù)FCB fOPEN_MAX; /FCB 拷貝OPENLIST()files=0;for(i nt i=O;i<OPEN_MAX;i+)fi.fatherBlockNum = -1;/

16、為分配打開fi.type=GENERAL;struct dirFile/* 目錄文件結(jié)構(gòu) */struct FCB fcbBlockFcbCou nt;void init(int _FatherBlockNum,int _CurrentBlockNum,char*name)/ 父塊號,當前塊號,目錄名strcpy(fcb0.fname,name); /本身的 FCBfcb0.fatherBlockNum=_FatherBlockNum;fcb0.curre ntBlockNum=_Curre ntBlockNum;fcb0.type=DIRECTORY; / 標記目錄文件for(i nt i=

17、1;i<BlockFcbCou nt;i+)標記為子項fcbi.fatherBlockNum=_Curre ntBlockNum; /fcbi.type=Zero; /標記為空白項; structDISK/ * int FAT1BlockCou nt;FAT1int FAT2BlockCou nt;/FAT2struct dirFile root; /根目錄char dataBlockCou nt-3BlockSize;void format()memset(FAT1,0,BlockCou nt);/FAT1memset(FAT2,0,BlockCou nt);/FAT2FAT1,FAT

18、2,根目錄區(qū)FAT10=FAT11=FAT12=-2; 0,1,2盤塊號依次代表FAT20=FAT21=FAT22=-2; /FAT 作備份 root.i ni t(2,2,"C:");根目錄區(qū)memset(data,0,sizeof(data);/ 數(shù)據(jù)區(qū);FILE *fp; /磁盤文件地址char * BaseAddr; /虛擬磁盤空間基地址string curren tPath="C:" /當前路徑in t curre nt=2; /當前目錄的盤塊號string cmd; /輸入指令struct DISK *osPoi nt; /磁盤操作系統(tǒng)指針c

19、har comma nd16; /文件名標識struct OPENLIST* ope nl ist; / 用戶文件列表指針int format();int mkdir(char *son fname);int rmdir(char *son fname);int create(char *n ame);in t listshow();int delfile(char *n ame);int cha ngePath(char *son fname);int write(char *n ame);int exit();int ope n( char *file);int close(char *f

20、ile);int read(char *file);/* 初始化*/ int format。curre nt = 2;curren tPath="C:" /當前路徑osPoi nt->format();打開文件列表初始化delete ope nlist;ope nlist= new OPENLIST;/* 保存到磁盤上 myfiles*/fp = fope n( FilePath,"w+");fwrite(BaseAddr,sizeof(char),DiskSize,fp);fclose(fp);printf(" 格式化成功! ! n”)

21、;return 1;intmkdir(char*sonfname)/* 倉U建 子 目 錄*/判斷是否有重名尋找空白子目錄項尋找空白盤塊號當前目錄下增加該子目錄項分配子目 錄盤塊,并且初始化修改fat表int i,temp,iFAT;struct dirFile *dir; /當前目錄的指針if(curre nt = 2) /根目錄dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);/* 為了避免該目錄下同名文件夾 */for(i = 1;i<BlockFcbCo

22、u nt;i+)if(dir->fcbi.type=DIRECTORY&&strcmp(dir->fcbi.fname,s on fname)=0 )printf("該文件夾下已經(jīng)有同名的文件夾存在了!n ”);return 0;for(i = 1;i < BlockFcbCount; i+)/查找空白 fcb 序號if(dir->fcbi.type=Zero)break;if(i = BlockFcbCou nt)printf(" 該目錄已滿!請選擇新的目錄下創(chuàng)建!n");return 0;temp = i;for(i =

23、 3;i < BlockCou nt;i+)if(osPoi nt->FAT1i = 0)break;if(i = BlockCou nt)printf(”磁盤已滿!n");return 0;iFAT=i;/* 接下來進行分配*/osPoi nt->FAT1iFAT=osPoi nt->FAT2iFAT = 2; 2表示分配給下級目錄文件/填寫該分派新的盤塊的參數(shù)strcpy(dir->fcbtemp.fname,s on fname);dir->fcbtemp.type=DIRECTORY;dir->fcbtemp.fatherBlockN

24、um=curre nt;dir->fcbtemp.curre ntBlockNum=iFAT;/初始化子目錄文件盤塊dir=(struct dirFile*)(osPoi nt->data iFAT-3); /定位到子目錄盤塊號dir->i nit (curre nt,iFAT,s on fname);/iFAT是要分配的塊號,這里的curre nt 用來指要分配的塊的父塊號printf(”創(chuàng)建子目錄成功!n ”);return 1;int rmdir(char *sonfname)/*刪除當前目錄下的文件夾 */int i,temp,j;/確保當前目錄下有該文件,并記錄下該

25、FCB下標struct dirFile *dir; /當前目錄的指針if(curre nt=2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);查找該目錄文件&&for(i=1;i<BlockFcbCou nt;i+)/if(dir->fcbi.type=DIRECTORY strcmp(dir->fcbi.fname,s on fname)=0) break;temp=i;if(i=BlockFcbCou nt)printf(”當

26、前目錄下不存在該子目錄!n");return 0;j = dir->fcbtemp.curre ntBlockNum;struct dirFile *so nDir; /當前子目錄的指針son Dir=(struct dirFile *)(osPoi nt->data j - 3);for(i=1;i<BlockFcbCou nt;i+) /查找子目錄是否為空目錄if(son Dir->fcbi.type!=Zero)printf(”該文件夾為非空文件夾,為確保安全,請清空后再刪除!n");return 0;/*開始刪除子目錄操作*/osPoi nt

27、->FAT1j = osPo in t->FAT2j=0;/fat清空char *p=osPoi nt->dataj-3;/格式化子目錄memset(p,0,BlockSize);dir->fcbtemp.i ni tialize();/回收子目錄占據(jù)目錄項prin tf("刪除當前目錄下的文件夾成功 n");return 1;/* 在當前目錄下創(chuàng)建文本文件 */int create(char *n ame)int i,iFAT;/temp,int emptyNum = 0,isFou nd = 0;/空閑目錄項個數(shù)struct dirFile *d

28、ir; /當前目錄的指針if(curre nt=2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);for(i=1;i<BlockFcbCount;i+)查看目錄是否已滿/為了避免同名的文本文件if(dir->fcbi.type = Zero && isF ound = 0)emptyNum = i;isF ound = 1;else if(dir->fcbi.type=GENERAL&&strcmp(dir->

29、;fcbi.fname,name)=0)printf("無法在同一目錄下創(chuàng)建同名文件!n");return 0;if(emptyNum = 0)prin tf("已經(jīng)達到目錄項容納上限,無法創(chuàng)建新目錄!n");return 0;for(i = 3;i<BlockCount;i+)查找FAT表尋找空白區(qū),用來分配磁盤塊號jif(osPoi nt->FAT1i=0)break;if(i=BlockCou nt)printf("磁盤已滿!n");return 0;iFAT=i;/*-進入分配階段*/分配磁盤塊osPoi nt-&

30、gt;FAT1iFAT = osPoi nt->FAT2iFAT = 1;/* 接下來進行分配*/填寫該分派新的盤塊的參數(shù)strcpy(dir->fcbemptyNum.fname, name); dir->fcbemptyNum.type=GENERAL; dir->fcbemptyNum.fatherBlockNum=curre nt; dir->fcbemptyNum.curre ntBlockNum=iFAT;dir->fcbemptyNum.size =0;char* p = osPoi nt->dataiFAT -3;memset(p,4,

31、BlockSize);printf(”在當前目錄下創(chuàng)建文本文件成功!n ”);return 1;/* 查詢子目錄*/in t listshow()int i,DirCou nt=0,FileCou nt=O;/搜索當前目錄struct dirFile *dir; /當前目錄的指針if(curre nt=2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);for(i=1;i<BlockFcbCou nt;i+)if(dir->fcbi.type=GENER

32、AL) /查找普通文件FileCou nt+;printf("%s文本文件.n",dir->fcbi.fname); if(dir->fcbi.type=DIRECTORY) /查找目錄文件DirCou nt+;printf("%s文件夾.n",dir->fcbi.fname);printf("n該目錄下共有 %d個文本文件,%d個文件夾nn",FileCount,DirCount);return 1;/* 在當前目錄下刪除文件 */int delfile(char *n ame)int i,temp,j;/確保當前

33、目錄下有該文件,并且記錄下它的FCB下標struct dirFile *dir; /當前目錄的指針if(curre nt = 2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);for(i=1;i < BlockFcbCou nt;i+) /查找該文件if(dir->fcbi.type=GENERAL && strcmp(dir->fcbi.fname ,n ame)=0) break;if(i = BlockFcbCou nt)p

34、rintf("當前目錄下不存在該文件!n");return 0;int k;for(k=0;k<OPEN_MAX;k+)if(ope nl ist->f k.type = GENERAL )&&(strcmp(ope nlist->f k.fname, name)=0)if(ope nlist->fk.fatherBlockNum = curre nt)break;elseprintf(”該文件未在當前目錄下!n");return 0;if(k!=OPEN_MAX)close( name);/從打開列表中刪除/*開始刪除文件

35、操作*/temp=i;j = dir->fcb temp.currentBlockNum ; /查找盤塊號 josPoi nt->FAT1j=osPoi nt->FAT2j=0;fat1,fat2表標記為空白char *p=osPo in t->dataj - 3;memset(p,0,BlockSize); /清除原文本文件的內(nèi)容dir->fcbtemp.i ni tialize(); type=O;/標記該目錄項為空文件prin tf("在當前目錄下刪除文件成功! n");return 1;/* 進入當前目錄下的子目錄 */int cha

36、ngePath(char *son fname)struct dirFile *dir; /當前目錄的指針if(curre nt=2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);/*回到父目錄*/if(strcmp(s on fname,".")=0)if(curre nt=2)prin tf("你現(xiàn)已經(jīng)在根目錄下!n");return 0;curre nt = dir->fcb0.fatherBlockNum ;c

37、urre ntPath=curre ntPath.substr(O,curre ntPath.size()strle n(dir->fcbO.fname )-1);return 1;/*進入子目錄*/確保當前目錄下有該目錄,并且記錄下它的FCB下標int i,temp;for(i = 1; i < BlockFcbCou nt; i+)/查找該文件if(dir->fcbi.type=DIRECTOR Y&&strcmp(dir->fcbi.fname,so nfname)=0) temp=i;break;if(i=BlockFcbCou nt)print

38、f("不存在該目錄!n");return 0;/修改當前文件信息curre nt=dir->fcb temp.curre ntBlockNum ;curre ntPath = curre ntPath+dir->fcb temp.fname +""printf("進入當前目錄下的子目錄成功!n");return 1;int exit()保存到磁盤上 C:myfiles/將所有文件都關(guān)閉 /*System exit*/fp=fope n(FilePath,"w+");fwrite(BaseAddr,siz

39、eof(char),DiskSize,fp);fclose(fp);II釋放內(nèi)存上的虛擬磁盤free(osPo in t);II釋放用戶打開文件表delete ope nlist;printf("退出文件系統(tǒng)成功!nn ”);return 1;int write(char *name)I* 在指定的文件里記錄信息 *1int i;char *startPo in t,*e ndPo int;II在打開文件列表中查找file(還需要考慮同名不同目錄文件的情況川)for(i=0;i<OPEN_MAX;i+)if(strcmp(ope nlist->f i.fname ,n a

40、me)=0 )if(ope nlist->fi.fatherBlockNum =curre nt)break;else!n");prin tf("該文件處于打開列表中,本系統(tǒng)只能改寫當前目錄下文件return 0;if(i=OPEN_MAX)printf("該文件尚未打開,請先打開后寫入信息!!n");return 0;int active=i;int fileStartNum = ope nlist->factive.curre ntBlockNum - 3 ;startPoi nt = osPoi nt->datafileStart

41、Num;en dPoi nt = osPoi nt->datafileStartNum + 1;printf("請輸入文本以Ctrl D號結(jié)束:t");char in put;while(i nput=getchar()!=4)if(startPoi nt < en dPoi nt-1) *startPo in t+ = in put;elseprin tf("達到單體文件最大容量!");*startPoi nt+ = 4;break;return 1;int read(char *file)/*選擇一個打開的文件讀取信息 */int i,f

42、ileStartNum;char *startPo in t,*e ndPo int;/struct dirFile *dir;/在打開文件列表中查找file(還需要考慮同名不同目錄文件的情況川)for(i=0;i<OPEN_MAX;i+)if(strcmp(ope nlist->f i.fname,file)=0 )if(ope nlist->fi.fatherBlockNum =curre nt)break;else!n");prin tf("該文件處于打開列表中,本系統(tǒng)只能閱讀當前目錄下文件return 0;if(i=OPEN_MAX)printf(

43、”該文件尚未打開,請先打開后讀取信息!n");return 0;int active=i;計算文件物理地址fileStartNum = ope nlist->factive.curre ntBlockNum - 3 ; startPoi nt = osPoi nt->datafileStartNum;en dPoi nt = osPoi nt->datafileStartNum + 1;printf("該文件的內(nèi)容為:”);while(*startPoi nt)!=4&& (startPoi nt < en dPoi nt)putch

44、ar(*startPo in t+);prin tf("n");return 1;int open(char *file)打開文件/*當前目錄下添加一個打開文件*/int i,Fcb In dex;/確保沒有打開過該文件=相同名字+相同目錄for(i=0;i<OPEN_MAX;i+)if(ope nlist->fi.type=GENERA& & strcmp(ope nlist->fi.fname,file)=0&&ope nlist->fi.fatherBlockNum = curre nt)prin tf(&quo

45、t;該文件已經(jīng)被打開!n");return 0;/確保有空的打開文件項if(ope nlist->files = OPEN_MAX)printf("打開文件數(shù)目達到上限!無法再打開新文件.n");return 0;/確保當前目錄下有該文件,并且記錄下它的FCB下標struct dirFile *dir; /當前目錄的指針if(curre nt=2)dir=&( osPo in t->root);elsedir=(struct dirFile *)(osPo in t->data curre nt-3);for(i = 1;i< Bl

46、ockFcbCou nt;i+)/查找該文件if(dir->fcbi.type=GENERAL && strcmp(dir->fcbi.fname,file)=O ) Feb In dex=i;break;if(i=BlockFcbCou nt)printf("當前目錄下不存在該文件!n");return 0;/裝載新文件進入打開文件列表,(FCB信息,文件數(shù)+) ?難道名字過不來?ope nlist->fOpe nF ileCou nt = dir->fcbFcbI ndex; /FCB拷貝ope nlist->files +

47、;printf("文件打開成功!n");Ope nF ileCou nt+;return 1;int close(char *file)/釋放該文件所占內(nèi)存/釋放用戶打開文件列表表項int i;/在打開文件列表中查找file(還需要考慮同名不同目錄文件的情況川)for(i=0;i<OPEN_MAX;i+)if(ope nlist->f i.type = GENERAL )&&(strcmp(ope nlist->f i.fname,file)=0)if(ope nlist->fi.fatherBlockNum = curre nt)b

48、reak;elseprintf(”該文件已打開,但未在當前目錄下,無法關(guān)閉!n");return 0;if(i=OPEN_MAX)printf("該文件未在打開列表中!n");return 0;int active=i;ope nlist->files -;ope nlist->factive.i nitialize();OpenF ileCo un t-;printf(”該文件已關(guān)閉!n");return 1;int mai n()prin tf(" Welcome To My Operate System Of File(FAT

49、)n ”);prin tf("n以下是使用說明書:n");使用說明書prin tf(" printf(" format :對磁盤格式化. n");printf(" exit :安全退出該文件系統(tǒng),保存信息. n”);prin tf(" mkdir dir name :創(chuàng)建子目錄. n”);prin tf(" rmdir dir name :刪除子目錄. n”);prin tf(" ls dir name :顯示當前目錄下信息. n");prin tf(" cddir name :更改當

50、前目錄. n");prin tf(" create file name :創(chuàng)建一個新文件,并且打開. n");prin tf(" write file name :選擇一個打開的文件寫入信息 n");prin tf(" read file name :選擇一個打開的文件讀取信息. n");prin tf(" rmfile name :刪除文件. n ”);prin tf(" ope n file name :打開文件.n");prin tf(" close file name :關(guān)閉文

51、件.n");prin tf(" ope nlist= new OPENLIST;/創(chuàng)建用戶文件打開表BaseAddr=(char *)malloc(DiskSize);申請?zhí)摂M空間并且初始化osPoi nt=(struct DISK *)(BaseAddr);虛擬磁盤初始化if(fp=fope n(FilePath,"r")!=NULL)加載磁盤文件fread(BaseAddr,sizeof(char),DiskSize,fp);printf("加載磁盤文件(%s )成功,現(xiàn)在可以進行操作了!nn",FilePath); elsepr

52、intf("這是你第一次使用該文件管理系統(tǒng)!t正在初始化.n");format();printf("初始化已經(jīng)完成,現(xiàn)在可以進行操作了!nn");while(1)cout<<curre ntPath;cin> >cmd; if(cmd="format")format();else if(cmd="mkd ir") cin> >comma nd;mkdir(comma nd);else if(cmd="rmdir")cin> >comma nd; rmdir(comma nd);

溫馨提示

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

評論

0/150

提交評論