操作系统试验模拟文件资料管理系统.docx

上传人:b****4 文档编号:24230113 上传时间:2023-05-25 格式:DOCX 页数:20 大小:21.34KB
下载 相关 举报
操作系统试验模拟文件资料管理系统.docx_第1页
第1页 / 共20页
操作系统试验模拟文件资料管理系统.docx_第2页
第2页 / 共20页
操作系统试验模拟文件资料管理系统.docx_第3页
第3页 / 共20页
操作系统试验模拟文件资料管理系统.docx_第4页
第4页 / 共20页
操作系统试验模拟文件资料管理系统.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

操作系统试验模拟文件资料管理系统.docx

《操作系统试验模拟文件资料管理系统.docx》由会员分享,可在线阅读,更多相关《操作系统试验模拟文件资料管理系统.docx(20页珍藏版)》请在冰豆网上搜索。

操作系统试验模拟文件资料管理系统.docx

操作系统试验模拟文件资料管理系统

【实验报告正文】

一、实验目的和要求(必填)

实验目的:

通过在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(charfilename[],intisdir);

voidcreateroot();

intrun();

intfindpath(char*topath);

voidhelp();

intmkdir();

intcreate();

intread();

intwrite();

intdel();

intrm();

intcd();

intdir();

filenode*root,*recent,*temp,*ttemp,*temp_child;

charpath[PATH_LENGTH],command[COMMAND_LENGTH],temppath[PATH_LENGTH],recentpath[PATH_LENGTH];

//创建文件或目录的存储节点

filenode*initnode(charfilename[],intisdir)

{

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("/",1);

root->parent=NULL;

root->child=NULL;

root->prev=root->next=NULL;

strcpy(path,"/");

}

voidhelp()

{

cout<

cout<<"create:

建立文件。

"<

cout<<"read:

读取文件。

"<

cout<<"write:

写入文件。

"<

cout<<"delete:

删除文件。

"<

cout<<"rm:

删除目录。

"<

cout<<"mkdir:

建立目录。

"<

cout<<"cd:

切换目录。

"<

cout<<"dir:

显示目录。

"<

cout<<"logout:

退出登录。

"<

}

intdir()

{

inti=0,j=0;

temp=newfilenode;

temp=recent;

if(temp==root)

{cout<<"

"<<"."<

if(temp!

=root)

{cout<<"

"<<".."<

if(temp->child==NULL)

{

cout<<"Total:

"<<"directors"<

return1;

}

temp=temp->child;

while(temp)

{

if(temp->isdir)

{cout<<"

"<filename<

else

{cout<<""<filename<

temp=temp->next;

}

cout<<"Total:

"<<"directors"<

return0;

}

intread()

{

charfilename[FILENAME_LENGTH];

cin>>filename;

if(recent->child==NULL)

{

cout<<"文件不存在!

"<

return1;

}

if(strcmp(recent->child->filename,filename)==0)

{

cout<child->content<

return0;

}

else

{

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{

cout<next->content<

return0;

}

}

cout<<"文件不存在!

"<

return1;

}

}

 

intwrite()

{

charfilename[FILENAME_LENGTH];

cin>>filename;

if(recent->child==NULL)

{

cout<<"文件不存在!

"<

return1;

}

if(strcmp(recent->child->filename,filename)==0)

{

cin>>recent->child->content;

cout<<"文件写入成功!

"<

return0;

}

else

{

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{

cin>>temp->next->content;

cout<<"文件写入成功!

"<

return0;

}

}

cout<<"文件不存在!

"<

return1;

}

}

intdel()

{

charfilename[FILENAME_LENGTH];

cin>>filename;

temp=newfilenode;

if(recent->child)

{

temp=recent->child;

while(temp->next&&(strcmp(temp->filename,filename)!

=0||temp->isdir!

=0))

temp=temp->next;

if(strcmp(temp->filename,filename)!

=0||temp->isdir!

=0)

{

cout<<"不存在该文件!

"<

return0;

}

}

else

{

cout<<"不存在该文件!

"<

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;

cout<<"文件已删除!

"<

return0;

}

intrm()

{

charfilename[FILENAME_LENGTH];

cin>>filename;

temp=newfilenode;

if(recent->child)

{

temp=recent->child;

while(temp->next&&(strcmp(temp->filename,filename)!

=0||temp->isdir!

=1))

temp=temp->next;

if(strcmp(temp->filename,filename)!

=0||temp->isdir!

=1)

{

cout<<"不存在该目录!

"<

return0;

}

}

else

{

cout<<"不存在该目录!

"<

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;

cout<<"目录已删除!

"<

return0;

}

 

intcd()

{chartopath[PATH_LENGTH];

cin>>topath;

if(strcmp(topath,".")==0)

return0;

if(strcmp(topath,"..")==0)

{

inti;

while(recent->prev)

recent=recent->prev;//向前回溯,找到第一次创建的目录

if(recent->parent)

{

recent=recent->parent;

}

i=strlen(path);

//printf("%d%s\n",i,path);

while(path[i]!

='/'&&i>0)

i--;//找到最右边的/

if(i!

=0)

{path[i]='\0';

//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,"/");

return0;

}

temp=recent;

strcpy(temppath,path);

if(topath[0]=='/')//cd命令以cd/开始

{

recent=root->child;

i++;

strcpy(path,"/");

//printf("\n%s",path);

}

else

{

if(recent!

=NULL&&recent!

=root)

{

strcat(path,"/");

//printf("\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]=='/'&&recent->child)

{

i++;

if(recent->isdir)

recent=recent->child;

else

{printf("路径错误\n");

return1;

}

strcat(path,"/");

}

while(topath[i]!

='/'&&i<=strlen(topath))

{

recentpath[j]=topath[i];

i++;j++;

}

recentpath[j]='\0';

while((strcmp(recent->filename,recentpath)!

=0||(recent->isdir!

=1))&&recent->next!

=NULL)

{

recent=recent->next;

}

if(strcmp(recent->filename,recentpath)==0)

{

if(recent->isdir==0)

{strcpy(path,temppath);

recent=temp;

printf("是文件不是目录。

\n");

return1;

}

strcat(path,recent->filename);

}

if(strcmp(recent->filename,recentpath)!

=0||recent==NULL)

{

strcpy(path,temppath);

recent=temp;

printf("输入路径错误\n");

return1;

}

}

return0;

}

intmkdir()

{

temp=initnode("",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("目录已存在!

\n");

return1;

}

}

while(ttemp->next)

{

ttemp=ttemp->next;

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)

{

printf("目录已存在!

\n");

return1;

}

}

ttemp->next=temp;

temp->parent=NULL;

temp->child=NULL;

temp->prev=ttemp;

temp->next=NULL;

printf("目录建立成功!

\n");

}

return0;

}

 

intcreate()

{

temp=initnode("",0);

cin>>temp->filename;

if(recent->child==NULL)

{

temp->parent=recent;

temp->child=NULL;

recent->child=temp;

temp->prev=temp->next=NULL;

cout<<"文件创建成功!

"<

}

else

{

ttemp=recent->child;

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0)

{

printf("文件已存在!

\n");

return1;

}

while(ttemp->next)

{

ttemp=ttemp->next;

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0)

{

printf("文件已存在!

\n");

return1;

}

}

ttemp->next=temp;

temp->parent=NULL;

temp->child=NULL;

temp->prev=ttemp;

temp->next=NULL;

cout<<"文件建立成功!

"<

}

return0;

}

 

intrun()

{

cout<<"filesystem:

"<";

cin>>command;

if(strcmp(command,"mkdir")==0)

mkdir();

elseif(strcmp(command,"dir")==0)

dir();

elseif(strcmp(command,"cd")==0)

cd();

elseif(strcmp(command,"create")==0)

create();

elseif(strcmp(command,"read")==0)

read();

elseif(strcmp(command,"rm")==0)

rm();

elseif(strcmp(command,"write")==0)

write();

elseif(strcmp(command,"delete")==0)

del();

elseif(strcmp(command,"help")==0)

help();

elseif(strcmp(command,"logout")==0)

return0;

else

cout<<"请参考help提供的命令列表!

"<

return1;

}

voidmain()

{

cout<<"***************************************************************"<

cout<<"********************操作系统课程设计项目*********************"<

cout<<"*简单文件系统模拟*"<

cout<<"*键入help可以获取帮助*"<

cout<<"***************************************************************"<

cout<<"***************************************************************"<

cout<

createroot();

while

(1)

{

if(!

run())

break;

}

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1