資源簡介
c++實現的文件系統 可以輸入指令創建文件刪除文件新建文件等
樹形結構 采用位示圖等 可查看占用磁盤情況 當然這些都是模擬的
當時做的大型實驗 帶實驗報告

代碼片段和文件信息
#include
#include
#include
using?namespace??std;
#define?MAX?1024
/*
*
*????Designed?by?wankaiming
*
*/
struct?fat
{
int?next; //下一個物理塊的地址
int?use; //位示圖?0?為空,1?不空
};
struct?fcb
{
int?first;??//第一個物理塊
int?last;???//最后一個物理塊
int?num;????//物理塊數
};
struct?file???//userFileDirectory???這里文件和文件夾均以這種類型來處理
{
char?name[20];???//文件或文件夾名
fcb?*fcb;
struct?file?*?brother;?????//文件的鏈表
struct?file?*?parent;??????//父目錄鏈表
struct?file?*?child;???????//子目錄鏈表
int?kind; //0文件夾;1文件
};
//----------------------------------------------------相關全局變量------------------------------------------//
fat?fat[MAX]; //?假設物理塊有1024個;
file?*?cur;//指向當前目錄的指針
int?rest=MAX;????//全局變量記錄剩余多少物理塊
//----------------------------------------------------相關全局變量------------------------------------------//
/*申請物理塊成功返回第一個物理塊的位置,返回-1時,表示申請物理塊不成功*/
void?distribute_block(file?*?u)
{
if(u->fcb->num<=rest)???//判斷文件控制塊中文件目錄需要的物理塊是不是比剩下的物理塊多
{
int?ij=0t=0;
for(i=0;ifcb->num;i++)???//i不超出總物理塊數,j小于需要的物理塊數
{
if(fat[i].use==0)???//根據位示圖?use==0說明此處為空
{
j++;
if(j==1)?????//分配第一個空物理塊
u->fcb->first=i;
else? //形成鏈表,可以根據fcb找到下一個物理塊
fat[t].next=i;
fat[i].use=1;??//分配一塊后置1表示位示圖此處不為空
rest--;???//剩余的空閑物理塊數要減1
t=i;
}
}
fat[i-1].next=0;??//作為文件使用的最后一個物理塊的標識
u->fcb->last=i-1;????//將信息記錄到該文件fcb中
}
else?
{
u->fcb->first=-1;??//表示沒有分配物理塊
cout<<“內存空間不足“;
}
}
/*初始化根目錄*/
void?init(){
file?*u;
u=new?file;???
u->fcb=new?fcb;
u->kind=1;
????strcpy(u->name“root“);
u->parent=NULL;
u->brother=NULL;
u->child=NULL;
cur=u;
}
/*顯示fat表*/
void?showfat()
{
int?i;
for(i=0;i<100;i++)
{
cout< if(i%10==9)?cout< else?cout<<‘?‘;
}
}
/*找到最后一個文件*/
file*?getLast(file*?father)???
{
file?*p=father->child;
while(p->brother!=NULL){
p=p->brother;?
}
return?p;
}
/*打印目錄下的內容*/
void?dir()
{
file?*p=cur->child;
cout<<“當前文件夾下的內容如下“< if(p!=NULL){
while(p!=NULL){
cout<<“文件名:“<name<<‘?‘<<“文件屬性:“<kind< ????p=p->brother;
}
}
else{
cout<<“當前目錄為空目錄“< }
}
/*創建文件*/
void?createfile()
{
file?*u*t;
u=new?file;???
u->fcb=new?fcb;
u->kind=1;
cout<<“請輸入名字“< ????cin>>u->name;
cout<<“請輸入需要的物理塊數“< cin>>u->fcb->num;
distribute_block(u);
if(u->fcb->first!=-1)
{
if(cur->child==NULL) //當前目錄沒有子內容直接將該文件目錄添加到鏈表中
cur->child=u;
else????//當前目錄有子內容,則將新產生的文件加入鏈表
{
t=getLast(cur);
t->brother=u;
}
u->brother=NULL;????
u->parent=cur;
u->child=NULL;
}
else???
{
delete?u;??????//first==-1表示因內存不足,無法創建目錄
}
}
/*創建目錄*/
void?createdir(){
file?*u*t;
u=new?file;???
u->fcb=new?fcb;
u->kind=0;
cout<<“請輸入名字“< cin>>u->name;
if(cur->child==NULL)
cur->child=u;
else
{
t=getLast(cur);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????117248??2012-06-15?12:04??實驗報告byWanKaiming.doc
?????文件???????8796??2012-06-15?12:33??1.cpp
-----------?---------??----------?-----??----
???????????????126044????????????????????2
- 上一篇:實現集合交并差運算c++
- 下一篇:Redis數據庫32位
評論
共有 條評論