1、实验四 文件系统实验实验四 文件系统实验一 . 目的要求 1、用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。 2、要求设计一个 n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。二 . 例题: 1、设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行用户可以打开5个文件。2、程序采用二级文件目录(即设置主目录MFD)和用户文件目录(UED)。另外,为打开文件设置了运行文
2、件目录(AFD)。3、为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。4、算法与框图: 因系统小,文件目录的检索使用了简单的线性搜索。 文件保护简单使用了三位保护码:允许读写执行、对应位为 1,对应位为0,则表示不允许读写、执行。 程序中使用的主要设计结构如下: 主文件目录和用户文件目录( MFD、UFD) 打开文件目录( AFD)(即运行文件目录)M D FU F DA F D用户名文件名打开文件名文件目录指针保护码打开保护码用户名文件长度读写指针文件目录指针文件名文件系统算法的流程图如下:三 . 实验题: 1、增加 23个文件操作命令,并加以实
3、现。(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别)。代码如下:/1、增加 23个文件操作命令,并加以实现。(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别)。#include#include#includeusing namespace std;struct TYPE_UFD string File_Name; bool Read; bool Write; bool Execute; int Length_File;struct TYPE_MFD string User_Name; TYPE_UFD *Pointer;struct TYPE_AFD int File
4、_ID; bool Read; bool Write; bool Execute; int Pointer;class TYPE_FILE_SYSTEMpublic: void Initial( void ); void Start( void );private: int _Number_Users; int _Number_Files; int _MaxNumber_Open_Files; TYPE_MFD *_MFD; TYPE_UFD *_UFD; TYPE_AFD *_AFD;void TYPE_FILE_SYSTEM:Initial( void ) _Number_Users =
5、10; _Number_Files = 10; _MaxNumber_Open_Files = 5; _UFD = new TYPE_UFD _Number_Users*_Number_Files; _MFD = new TYPE_MFD _Number_Users; int i=0; for( i=0 ; i_Number_Users ; i+ ) _MFDi.Pointer = &(_UFDi*_Number_Files); _AFD = new TYPE_AFD _MaxNumber_Open_Files; _MFD0.User_Name = zaq; _UFD0.File_Name =
6、 file1.txt; _UFD0.Length_File = 10; _UFD0.Read = true; _UFD0.Write = false; _UFD0.Execute = true; _UFD1.File_Name = file2.txt; _UFD1.Length_File = 20; _UFD1.Read = true; _UFD1.Write = false; _UFD1.Execute = false; for( i=2 ; i_Number_Files ; i+ ) _UFDi.File_Name = ; _UFDi.Length_File = -1; _UFDi.Rea
7、d = false; _UFDi.Write = false; _UFDi.Execute = false; void TYPE_FILE_SYSTEM:Start( void ) int User_ID; int i,temp_int; string temp; char choice; int Number_Open_Files; string User_Name; string Command; TYPE_UFD *UFD; do do cout 已创建用户名为zaqn指令有:create delete open dir diropen write read logout shutdow
8、n nn; cout User_Name; for( User_ID=0 ; User_ID_Number_Users ; User_ID+ ) if( _MFDUser_ID.User_Name = User_Name ) break; if( User_ID = _Number_Users ) cout 用户名错误,请再次输入 . endl; while( User_ID = _Number_Users ); cout 欢迎登录 , User_Name ! endl; UFD = _MFDUser_ID.Pointer; for( i=0 ; i_MaxNumber_Open_Files
9、; i+ ) _AFDi.File_ID = -1; Number_Open_Files = 0; do cout C: User_Name ; cin Command; if( Command = dir ) cout endl; cout 打开用户 User_Name 的文件 endl; cout t Statet Lengtht File name endl; for( i=0 ; i_Number_Files ; i+ ) if( UFDi.Length_File != -1 ) cout t ; if( UFDi.Read = true ) cout R; else cout -;
10、if( UFDi.Write = true ) cout W; else cout -; if( UFDi.Execute = true ) cout E; else cout -; cout t; cout UFDi.Length_File; cout t; cout UFDi.File_Name endl; cout endl; else if( Command = diropen ) cout endl; cout 打开用户 User_Name 的文件 endl; cout t Statet Open File name endl; for( i=0 ; i_MaxNumber_Open
11、_Files ; i+ ) if( _AFDi.File_ID != -1 ) cout t ; if( _AFDi.Read = true ) cout R; else cout -; if( _AFDi.Write = true ) cout W; else cout -; if( _AFDi.Execute = true ) cout E; else cout -; cout t; cout UFD_AFDi.File_ID.File_Name endl; cout endl; else if( Command = create ) for( i=0 ; i_Number_Files ;
12、 i+ ) if( UFDi.Length_File = -1 ) break; if( i = _Number_Files ) cout Error: 已有名为 _Number_Files 的文件. endl; else cout 请输入新文件信息: endl; cout temp; UFDi.File_Name = temp; cout 文件权限 : ; cout Read (y/n):; do choice = getch(); while( choice!=y & choice!=n ); if( choice = y ) UFDi.Read = true; else UFDi.Rea
13、d = false; cout endl; cout Write (y/n):; do choice = getch(); while( choice!=y & choice!=n ); if( choice = y ) UFDi.Write = true; else UFDi.Write = false; cout endl; cout Execute (y/n):; do choice = getch(); while( choice!=y & choice!=n ); if( choice = y ) UFDi.Execute = true; else UFDi.Execute = fa
14、lse; cout endl; cout temp_int; if( temp_int 0 ) UFDi.Length_File = temp_int; cout 新文件 UFDi.File_Name 已建立! endl; else if( Command = delete ) cout temp; for( i=0 ; i_Number_Files ; i+ ) if( (UFDi.Length_File!=-1)&(UFDi.File_Name=temp) ) break; if( i = _Number_Files ) cout 文件名错误,请再次输入 . endl; else UFDi
15、.Length_File = -1; cout 文件 UFDi.File_Name 已删除 . endl; else if( Command = open ) if( Number_Open_Files = _MaxNumber_Open_Files ) cout Error: 你已经打开了 Number_Open_Files 文件. endl; else cout temp; for( i=0 ; i_Number_Files ; i+ ) if( (UFDi.Length_File!=-1)&(UFDi.File_Name=temp) ) break; if( i = _Number_Fi
16、les ) cout 文件名错误,请再次输入 . endl; else Number_Open_Files+; for( temp_int=0 ; temp_int_MaxNumber_Open_Files ; temp_int+ ) if( _AFDtemp_int.File_ID = -1 ) break; _AFDtemp_int.File_ID = i; _AFDtemp_int.Pointer = 0; cout 请定义打开方式 : endl; if( UFDi.Read = true ) cout Read (y/n):; do choice = getch(); while( c
17、hoice!=y & choice!=n ); if( choice = y ) _AFDtemp_int.Read = true; else _AFDtemp_int.Read = false; cout endl; else _AFDtemp_int.Read = false; if( UFDi.Write = true ) cout Write (y/n):; do choice = getch(); while( choice!=y & choice!=n ); if( choice = y ) _AFDtemp_int.Write = true; else _AFDtemp_int.
18、Write = false; cout endl; else _AFDtemp_int.Write = false; if( UFDi.Execute = true ) cout Execute (y/n):; do choice = getch(); while( choice!=y & choice!=n ); if( choice = y ) _AFDtemp_int.Execute = true; else _AFDtemp_int.Execute = false; cout endl; else _AFDtemp_int.Execute; cout 文件 temp 已打开 . end
19、l; else if( Command = logout ) cout 再见 , User_Name ! endl; break; else if( Command = close ) cout temp; for( i=0 ; i_Number_Files ; i+ ) if( (UFDi.Length_File!=-1)&(UFDi.File_Name=temp) ) break; if( i = _Number_Files ) cout 文件名错误,请再次输入 . endl; else for( temp_int=0 ; temp_int_MaxNumber_Open_Files ; t
20、emp_int+ ) if( _AFDtemp_int.File_ID = i ) break; if( temp_int = _MaxNumber_Open_Files ) cout 文件 temp 未打开 . endl; else _AFDtemp_int.File_ID = -1; Number_Open_Files-; cout 文件 temp 已关闭 . endl; else if( Command = read ) cout temp; for( i=0 ; i_Number_Files ; i+ ) if( (UFDi.Length_File!=-1)&(UFDi.File_Na
21、me=temp) ) break; if( i = _Number_Files ) cout 文件名错误,请再次输入 . endl; else for( temp_int=0 ; temp_int_MaxNumber_Open_Files ; temp_int+ ) if( _AFDtemp_int.File_ID = i ) break; if( temp_int = _MaxNumber_Open_Files ) cout 文件 temp 未打开 . endl; else if( _AFDtemp_int.Read = true ) cout 文件 temp 成功读取. endl; els
22、e cout Error: 文件打开模式错误 . endl; else if( Command = write ) cout temp; for( i=0 ; i_Number_Files ; i+ ) if( (UFDi.Length_File!=-1)&(UFDi.File_Name=temp) ) break; if( i = _Number_Files ) cout 文件名错误,请再次输入 . endl; else for( temp_int=0 ; temp_int_MaxNumber_Open_Files ; temp_int+ ) if( _AFDtemp_int.File_ID
23、 = i ) break; if( temp_int = _MaxNumber_Open_Files ) cout 文件 temp 未打开 . endl; else if( _AFDtemp_int.Write = true ) cout 文件 temp 成功写入. endl; else cout Error: 文件打开模式错误 . endl; else if( Command = shutdown ) cout 正在注销. endl; cout 再见 , User_Name ! endl; cout 正在关机. endl; break; else cout 指令错误,请再次输入 . endl
24、; while( Command != logout & Command != shutdown ); while( Command != shutdown );int main() TYPE_FILE_SYSTEM FS; FS.Initial(); FS.Start(); return 0;2、编一个通过屏幕选择命令的文件管理系统,每屏要为用户提供足够的选择信息,不需要打入冗长的命令。 代码如下:#include using namespace std;/2、编一个通过屏幕选择命令的文件管理系统,每屏要为用户提供足够的选择信息,不需要打入冗长的命令。#include #include #i
25、nclude #define getpch(type) (type*)malloc(sizeof(type)void Select();int userNum=0;struct mdf char userName20; struct ufd* p; mdf20;struct ufd char fileName20; char File50; struct ufd * next;*fp,*tp,*p,*begin;typedef struct ufd UFD;void show( UFD *f) begin=f; if(begin-next=NULL) printf(该用户名下尚无文件!n); else printf(该用户名下所有文件:n); begin=begin-next; while(begin!=NULL)
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2