




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验名称: 文件管理 姓名: 俞家阳 学号: 1526048 第17页装 订 线【实验报告正文】一、实验目的和要求(必填)实验目的:通过在VC平台下编程,设计和调试一个简单的文件系统,通过模拟文件操作命令的执行,来模拟文件系统对文件及目录的管理。实验要求:两名学生成组结对完成实验,仿真出文件系统中对文件和目录的操作。二、实验内容(必填)文件管理:实现一个简单的文件系统三、实验原理或实验方法(必填)原理:通过结构体来描述文件和目录,利用链表知识实现目录树结构,通过对链表的操作实现整个文件系统中目录和文件的相关操作。方法:学生两人结对进行实验,分别实现对文件和目录的操作。对文件的操作包括:创建文件
2、create、读文件read、写文件write、删除文件delete。对目录的操作包括:创建目录mkdir、切换目录cd、展示目录内容dir、删除目录rm。四、主要仪器设备或实验条件Windows操作系统,VC开发环境五、实验步骤(含实验数据记录处理)或操作设计过程记录#include "stdio.h"#include "iostream.h"#include "string.h"#define FILENAME_LENGTH 10 /文件名称长度#define COMMAND_LENGTH 10 /命令行长度#define PAT
3、H_LENGTH 30 /参数长度struct filenode char filenameFILENAME_LENGTH; int isdir; char content255; filenode *parent; filenode *child; filenode *prev; filenode *next;filenode *initnode(char filename,int isdir); void createroot(); int run(); int findpath(char *topath); void help(); int mkdir(); int create();
4、int read(); int write(); int del(); int rm(); int cd(); int dir();filenode *root,*recent,*temp,*ttemp,*temp_child;char pathPATH_LENGTH,commandCOMMAND_LENGTH,temppathPATH_LENGTH,recentpathPATH_LENGTH;/创建文件或目录的存储节点filenode* initnode(char filename,int isdir) filenode *node=new filenode; strcpy(node->
5、;filename,filename); node->isdir=isdir;node->parent=NULL; node->child=NULL; node->prev=NULL;node->next=NULL;return node;/初始化文件系统根结点void createroot () recent=root=initnode("/",1); root->parent=NULL; root->child=NULL; root->prev=root->next=NULL; strcpy(path,"/
6、"); void help() cout<<endl; cout<<"create: 建立文件。 "<<endl; cout<<"read: 读取文件。 "<<endl; cout<<"write: 写入文件。 "<<endl; cout<<"delete: 删除文件。 "<<endl; cout<<"rm: 删除目录。 "<<endl; cout<
7、<"mkdir: 建立目录。 "<<endl; cout<<"cd: 切换目录。 "<<endl; cout<<"dir: 显示目录。 "<<endl; cout<<"logout: 退出登录。 "<<endl;int dir()int i=0,j=0;temp=new filenode;temp=recent;if(temp=root)cout<<" <DIR> "<<
8、"."<<endl; if(temp!=root)cout<<" <DIR> "<<"."<<endl;i+;if(temp->child=NULL) cout<<"Total: "<<" directors " <<i<<" files "<< j <<endl; return 1;temp=temp->child;while(te
9、mp) if(temp->isdir) cout<<" <DIR> "<<temp->filename<<endl;i+; else cout<<" <FILE> "<<temp->filename<<endl;j+; temp=temp->next; cout<<"Total: "<<" directors " <<i<<" files
10、"<< j <<endl;return 0;int read()char filenameFILENAME_LENGTH;cin>>filename; if(recent->child=NULL) cout<<"文件不存在!"<<endl; return 1; if(strcmp(recent->child->filename,filename)=0) cout<<recent->child->content<<endl; return 0; else
11、 temp=recent->child; while(temp->next) if(strcmp(temp->next->filename,filename)=0) cout<<temp->next->content<<endl; return 0; cout<<"文件不存在!"<<endl; return 1; int write()char filenameFILENAME_LENGTH;cin>>filename; if(recent->child=NULL) cou
12、t<<"文件不存在!"<<endl; return 1; if(strcmp(recent->child->filename,filename)=0) cin>>recent->child->content; cout<<"文件写入成功!"<<endl; return 0; else temp=recent->child; while(temp->next) if(strcmp(temp->next->filename,filename)=0) c
13、in>>temp->next->content; cout<<"文件写入成功!"<<endl; return 0; cout<<"文件不存在!"<<endl; return 1; int del() char filenameFILENAME_LENGTH; cin>>filename; temp=new filenode; if(recent->child) temp=recent->child; while(temp->next &&
14、 (strcmp(temp->filename,filename)!=0 | temp->isdir!=0) temp=temp->next; if(strcmp(temp->filename,filename)!=0 | temp->isdir!=0) cout<<"不存在该文件!"<<endl; return 0; else cout<<"不存在该文件!"<<endl; return 0; if(temp->parent=NULL) temp->prev->
15、;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; delete temp; cout<<"文件已删除!"<<endl; return 0;int rm() char fil
16、enameFILENAME_LENGTH; cin>>filename; temp=new filenode; 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<<"不存在
17、该目录!"<<endl; return 0; else cout<<"不存在该目录!"<<endl; return 0; 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-
18、>parent; temp->parent->child=temp->next; delete temp; cout<<"目录已删除!"<<endl; return 0;int cd() char topathPATH_LENGTH; cin>>topath; if(strcmp(topath,".")=0) return 0; if(strcmp(topath,".")=0) int i; while(recent->prev)recent=recent->pr
19、ev; /向前回溯,找到第一次创建的目录if(recent->parent) recent=recent->parent; i=strlen(path);/ printf("%d %sn",i,path); while(pathi!='/' && i>0) i-; /找到最右边的/if(i!=0) pathi='0' /printf("%s",path); /path中不止有一个/else pathi+1='0' elsefindpath(topath); return 0
20、;int findpath(char *topath) unsigned int i=0; int sign=1; if(strcmp(topath,"/")=0) /如果命令是cd / recent=root; strcpy(path,"/"); return 0; temp=recent; strcpy(temppath,path); if(topath0='/') /cd命令以cd /开始 recent=root->child; i+; strcpy(path,"/");/printf("n%s&
21、quot;,path); else if(recent!=NULL && recent!=root) strcat(path,"/");/ printf("n%sn",path); if(recent && recent->child)if(recent->isdir) recent=recent->child;elseprintf("路径错误!n");return 1; while(i<=strlen(topath) && recent) int j=0; if
22、(topathi='/' && recent->child) i+; if(recent->isdir) recent=recent->child; else printf("路径错误n"); return 1; strcat(path,"/"); while(topathi!='/' && i<=strlen(topath) recentpathj=topathi; i+;j+; recentpathj='0' while(strcmp(recent
23、->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"); return 1; strcat(path,recent->filename); if(strcmp(
24、recent->filename,recentpath)!=0 | recent=NULL) strcpy(path,temppath); recent=temp; printf("输入路径错误n"); return 1; return 0;int mkdir() temp=initnode(" ",1); cin>>temp->filename;if(recent->child=NULL) temp->parent=recent; temp->child=NULL; recent->child=temp;
25、 temp->prev=temp->next=NULL;printf("目录建立成功!n"); else ttemp=recent->child;if(strcmp(ttemp->filename,temp->filename)=0&&ttemp->isdir=1) printf("目录已存在!n"); return 1; while(ttemp->next) ttemp=ttemp->next; if(strcmp(ttemp->filename,temp->filename)
26、=0&&ttemp->isdir=1) printf("目录已存在!n"); return 1; ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; printf("目录建立成功!n"); return 0;int create() temp=initnode(" ",0); cin>>temp->filename; if(recent-&
27、gt;child=NULL) temp->parent=recent; temp->child=NULL; recent->child=temp; temp->prev=temp->next=NULL; cout<<"文件创建成功!"<<endl; else ttemp=recent->child; if(strcmp(ttemp->filename,temp->filename)=0&&ttemp->isdir=0) printf("文件已存在!n"); re
28、turn 1; while(ttemp->next) ttemp=ttemp->next; if(strcmp(ttemp->filename,temp->filename)=0&&ttemp->isdir=0) printf("文件已存在!n"); return 1; ttemp->next=temp; temp->parent=NULL; temp->child=NULL; temp->prev=ttemp; temp->next=NULL; cout<<"文件建立成功!"<<endl; return 0;int run() cout<<"filesystem:"<<path<<">
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 流动货摊的城乡一体化实践考核试卷
- 海洋油气开采工程技术难点与解决方案考核试卷
- 塑料注塑件脱模剂应用考核试卷
- 机床制造业品牌建设与市场推广考核试卷
- 煤炭产业转型升级与市场机遇把握考核试卷
- 滑动轴承的智能制造与大数据分析考核试卷
- 林业节能减排政策效应分析考核试卷
- 木片加工废弃物资源化利用考核试卷
- 木片在环保家具市场的竞争力分析考核试卷
- 合成橡胶在航空航天器部件的应用考核试卷
- 人教版一年级下20以内退位减法
- 《雷达原理》课件-3.5.4教学课件:ISAR雷达工作原理
- 新版环氧乙烷安全技术说明书SDS模版
- 衡中同卷2025届高三第一次模拟考试数学试卷含解析
- GB/T 22884-2024皮革牛蓝湿革规范
- 经济学系劳动经济学课程教学大纲
- 液压与气压传动习题及参考答案
- 2024-2030年奶豆腐行业市场现状供需分析及投资评估规划分析研究报告
- 广西靖西绣球文化
- 2024年江苏苏州市(12345)便民服务中心招聘座席代表人员易考易错模拟试题(共500题)试卷后附参考答案
- 2022年山东省职业院校技能大赛高职组导游服务赛项题库
评论
0/150
提交评论