版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【实验报告正文】
一、实验目的和要求(必填)
实验目的:通过在VC平台下编程,设计和调试一个简单的文件系统,通过模拟文件操作命
令的执行,来模拟文件系统对文件及目录的管理。
实验要求:两名学生成组结对完成实验,仿真出文件系统中对文件和目录的操作。
二、实验内容(必填)
文件管理:实现一个简单的文件系统
装三、实验原理或实验方法(必填)
订
原理:通过结构体来描述文件和目录,利用链表知识实现目录树结构,通过对链表的操作实
我现整个文件系统中目录和文件的相关操作。
方法:学生两人结对进行实验,分别实现对文件和目录的操作。
对文件的操作包括:创建文件create、读文件read、写文件write、删除文件delete。
对目录的操作包括:创建目录mkdir、切换目录cd、展示目录内容dir、删除目录rm。
四、主要仪器设备或实验条件
Windows操作系统,VC开发环境
五、实验步骤(含实验数据记录处理)或操作设计过程记录
#include"stdio.h"
#include^iostream.h*'
#include"string.h”
#defineFILENAME_LENGTH10〃文件名称长度
#defineCOMMAND.LENGTH10〃命令行长度
#definePATH_LENGTH30〃参数长度
structfilenode
charfilename[FILENAME_LENGTH];
intisdir;
charcontent[255];
filenode*parent;
filenode*child;
filenode*prev;
filenode*next;
);
filenode*initnode(charfilename1],intisdir);
voidcreateroot();
intrun();
intfindpath(char*topath);
voidhelp();
intmkdir();
intcreate();
intread();
intwrite();
intdel();
intrm();
intcd();
intdir();
filenode*root,*recent,*temp,*ttemp,*temp_child;
char
path[PATH_LENGTH],command[COMMAND_LENGTH],temppath[PATH_LENGTH],recen
tpath[PATH_LENGTH];
〃创建文件或目录的存储节点
filenode*initnode(charisdir)
filenode*node=newfilenode;
strcpy(node->filename,filename);
node->isdir=isdir;
node->parent=NULL;
node->child=NULL;
node->prev=NULL;
node->next=NULL;
returnnode;
)
〃初始化文件系统根结点
voidcreateroot()
(
recent=root=initnode(T,1);
root->parent=NULL;
root->child=NULL;
root-〉prev=root->next=NULL;
strcpy(path,,,r);
)
voidhelp()
(
cout«endl;
cout«ncreate:建立文件。"«endl;
cout«nread:读取文件。H«endl;
cout«uwrite:写入文件。n«endl;
cout«ndelete:删除文件。n«endl;
cout«nrm:删除目录。"«endl;
cout«nmkdir:建立目录。"«endl;
cout«ncd:切换目录。"«endl;
cout«ndir:显示目录。"«endl;
cout«*'logout:退出登录。',«endl;
)
intdir()
(
inti=O,j=O;
temp=newfilenode;
temp=recent;
if(temp==root)
{cout«H<DIR>,,«M.,,«endl;}
if(temp!=root)
{cout«M<DIR>,'«H.."«endl;i++;}
if(temp->child==NULL)
(
cout«,rTotal:"«"directors«i«Mfiles"«j«endl;
return1;
)
temp=temp->child;
while(temp)
(
if(temp->isdir)
{cout«"<DIR>n«temp->filename«endl;i++;}
else
{cout«M<FILE>,,«temp->filename«endl;j++;}
temp=temp->next;
)
cout«*Total:directors“«!«•'files*'«j«endl;
return0;
)
intread()
charfilename[FILENAME_LENGTH];
cin»filename;
if(recent->child==NULL)
(
coutw”文件不存在!”v〈endl;
return1;
)
if(strcmp(recent->child->filename,filename)==O)
(
cout«recent->child->content«endl;
return0;
}
else
(
temp=recent->child;
while(temp->next)
(
if(strcmp(temp->next->filename,filename)==O)
(
cout«temp->next->content«endl;
return0;
)
}
coutw”文件不存在!”v<endl;
return1;
}
)
intwrite()
charfilename[FILENAME_LENGTH];
cin»filename;
if(recent->child==NULL)
(
coutw”文件不存在!”v〈endl;
return1;
)
if(strcmp(recent->child->filename,filename)==O)
(
cin»recent->child->content;
8ut«”文件写入成功ivvendl;
return0;
)
else
(
tGmp=recent->child;
while(temp->next)
(
if(strcmp(temp->next->filename,filename)==O)
(
cin»temp->next->content;
8Utvv”文件写入成功Fvvendl;
return0;
}
)
coutw”文件不存在!”vvendl;
return1;
)
)
intdel()
(
charfilename[FILENAME_LENGTH];
cin»filename;
temp=newfilenode;
if(recent->child)
(
temp=recent->child;
while(temp->next&&(strcmp(temp->filename,filename)!=O||temp->isdir!=O))
temp=temp->next;
if(strcmp(temp->filename,filename)!=O||temp->isdir!=O)
(
coutvv”不存在该文件!M«endl;
return0;
}
)
else
(
coutw”不存在该文件IM«endl;
return0;
)
if(temp->parent==NULL)
(
temp->prev->next=temp->next;
if(temp->next)
temp->next->prev=temp->prev;
temp->prev=temp->next=NULL;
)
else
(
if(temp->next)
temp->next->parent=temp->parent;
temp->parent->child=temp->next;
)
deletetemp;
coutvv”文件已删除Fvvendl;
return0;
)
intrm()
(
charfilename[FILENAME_LENGTH];
cin»filename;
temp=newfilenode;
if(recent->child)
(
temp=recent->child;
while(temp->next&&(strcmp(temp->filename,filename)!=O||temp->isdir!=1))
temp=temp->next;
if(strcmp(temp->filename,filename)!=O||temp->isdir!=1)
(
coutw"不存在该目录!"vvendl;
return0;
else
coutvv"不存在该目录!"vvendl;
return0;
)
if(temp->parent==NULL)
(
temp->prev->next=temp->next;
if(temp->next)
temp->next->prev=temp->prev;
temp->prev=temp->next=NULL;
}
else
(
if(temp->next)
temp->next->parent=temp->parent;
temp->parent->child=temp->next;
)
deletetemp;
8ut〈v”目录已删除!”"endl;
return0;
)
intcd()
{chartopath[PATH_LENGTH];
cin»topath;
if(strcmp(topath,H.")==0)
return0;
if(strcmp(topath,H..")==0)
inti;
while(recent->prev)
recent=recent・>prev;//向前回溯,找到第一次创建的目录
if(recent->parent)
(
recent=recent->parent;
)
i=strlen(path);
//printf(H%d%s\n",i,path);
while(path[q!=7&&i>0)
〃找到最右边的/
if(i!=O)
{path[i]=*\O';
//printf(,,%s,,,path);//path中不止有一个/
)
else
path[i+1]-\0';
)
else
(
findpath(topath);
)
return0;
)
intfindpath(char*topath)
unsignedinti=0;
intsign=1;
if(strcmp(topath")==0)〃如果命令是cd/
(
recent=root;
strcpy(path,T);
return0;
)
temp=recent;
strcpy(temppath,path);
if(topath[0]==P)〃cd命令以cd/开始
(
recent=root->child;
i++;
strcpy(path.T);
//printf("\n%s",path);
)
else
(
if(recent!=NULL&&recent!=root)
(
strcat(path,'*/");
//printf(w\n%s\n',,path);
)
if(recent&&recent->child)
(
if(recent->isdir)
recent=recent->child;
else
printf("路径错误!\n");
return1;
)
)
}
while(i<=strlen(topath)&&recent)
(
intj=0;
if(topath[i]==7&&recent->child)
(
i++;
if(recent->isdir)
recent=recent->child;
else
{printf("路径错误\n”);
return1;
)
strcat(path,T);
)
while(topath[i]!=7&&i<=strlen(topath))
(
recentpathO]=topath[i];
i++;j++;
)
recentpath[i]=,\O';
while((strcmp(recent->filename,recentpath)!=O(recent->isdir!=1))&&
recent->next!=NULL)
(
recent=recent->next;
)
if(strcmp(recent->filename,recentpath)==O)
if(recent->isdir==O)
{strcpy(path,temppath);
recent=temp;
printf("是文件不是目录。\nH);
return1;
)
strcat(path,recent->filename);
)
if(strcmp(recent->filename,recentpath)!=O||recent==NULL)
(
strcpy(path,temppath);
recent=temp;
printf("输入路径错误\rf);
return1;
)
)
return0;
)
intmkdir()
(
temp=initnode("n,1);
cin»temp->filename;
if(recent->child==NULL)
(
temp->parent=recent;
temp->child=NULL;
recent->child=temp;
temp->prev=temp->next=NULL;
printf("目录建立成功!\n“);
)
else
ttemp=recent->child;
if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)
(
(
printf("目录已存在!\nn);
return1;
)
)
while(ttemp->next)
(
ttemp=ttemp->next;
if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)
(
printf(”目录已存在!5");
return1;
)
)
ttemp->next=temp;
temp->parent=NULL;
temp->child=NULL;
temp->prev=ttemp;
temp->next=NULL;
printf("目录建立成功!\n");
)
return0;
)
intcreate()
temp=initnode(H",0);
cin»temp->filename;
if(recent->child==NULL)
(
temp->parent=recent;
temp->child=NULL;
recent->child=temp;
temp->prev=temp->next=NULL;
coutvv”文件创建成功F«endl;
)
else
(
ttemp=recent->child;
if(strcmp(ttemp->filename,temp->filename)==O&&ttemp->isdir==O)
(
printf("文件已存在!\n”);
return1;
)
while(ttemp->next)
(
ttemp=ttemp->next;
if(strcmp(ttemp->filename,temp->filename)==O&&ttemp->isdir==O)
(
printf("文件已存在!\n”);
return1;
)
)
ttemp->next=temp;
temp->parent=NULL;
temp->child=NULL:
temp->prev=ttemp;
temp->next=NULL;
coutvv”文件建立成功FvvendK
)
return0;
)
intrun()
(
cout«"filesystem:n«path«,,>n;
cin»command;
if(strcmp(command,"mkdir)==0)
mkdir();
elseif(strcmp(command,"d
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电力工程造价员预算编制考试题目及答案
- 充电桩运维员设备维护考试题目及答案
- 卡尔多炉工安全生产意识竞赛考核试卷含答案
- 化工工艺试验工冲突解决能力考核试卷含答案
- 玻璃制品冷加工工安全生产基础知识强化考核试卷含答案
- 架线维护工复试评优考核试卷含答案
- 景泰蓝磨蓝工安全意识竞赛考核试卷含答案
- 农产品品相管理员变革管理知识考核试卷含答案
- 经济理论与实务2026年备考练习题
- 烟草物理检验员10S执行考核试卷含答案
- 辅警招聘考试300题及答案
- 高中心理健康教育-【9 化解冲突“圈”住朋友】
- 安全标志平面布置图
- 锂离子电池及锂离子电池正极材料钴酸锂的研发及产业化项目环境影响报告书
- 一-工地试验室建设及管理培训方案课件
- 生物表面活性剂鼠李糖脂
- GA 1809-2022城市供水系统反恐怖防范要求
- 宏观经济形势及投资分析课件
- GB/T 14194-2017压缩气体气瓶充装规定
- 材料学 印模材料-口腔专业课课件-口腔材料
- MicrosoftAzure云安全应用场景教学课件
评论
0/150
提交评论