#include"stdio.h"
#include"string.h"
typedef struct _directory{
char name[20];
int begin;
int length;
char owner[10];
}Directory;
FILE *dir,*file;
void ls(){
int count=10;
Directory dirs;
fseek(dir,0,0);/*把文件指针放到文件的开头*/
while(count>0){
count=fread(&dirs,sizeof(Directory),1,dir);/*读目录文件*/
if(count>0)
printf("%s\n",dirs.name);
}
}
void cat(char name[])
{
int count=10,begin,length;
char content[100];/*文件的最大长度是100个字符*/
Directory dirs;
fseek(dir,0,0);
while((count>0)&&(strcmp(dirs.name,name)!=0))
{/*查目录*/
count=fread(&dirs,sizeof(Directory),1,dir);
}
if(count==0){
printf("no such file!");
return;
}
else{/*创建该文件*/
begin=dirs.begin;
length=dirs.length;
fseek(file,begin,0);
fread(&content,1,length,file);
content[length]='\0';
printf("%s",content);
printf("\n");
}
}
void create(char name[]){
char content[100];
Directory dirs;
fseek(dir,0,2);/*将文件指针放到文件结束的位置*/
strcpy(dirs.name,name);
fseek(file,0,2);
scanf("%s",&content);
dirs.length=strlen(content);
dirs.begin=ftell(file);
fwrite(&dirs,sizeof(dirs),1,dir);
fwrite(&content,1,dirs.length,file);
}
void rm(char name[]){
}
void main(){
char name[20];
char *tempname;
file=fopen("file.dat","a+");/*打开数据文件*/
dir=fopen("directory.dat","a+");/*打开目录文件*/
char temp[20];
while(strcmp(temp,"exit")!=0){
gets(temp);/*从命令行中读字符串*/
if(strstr(temp,"ls")!=0)
ls();/*列出所有文件*/
else if(strstr(temp,"cat")!=0){
tempname=temp+4;
strcpy(name,tempname);
cat(name);/*读指定文件中的内容*/
}
else if(strstr(temp,"create")){
tempname=temp+7;
strcpy(name,tempname);
create(name);/*创建一个文件*/
}
else if(strstr(temp,"rm")){
tempname=temp+7;
strcpy(name,tempname);
rm(name);/*删除一个文件*/
}
else printf("bad order");
printf("\n");
}
fclose(dir);/*关闭一个文件*/
fclose(file);
}
操作系统模拟实现单级目录的文件系统
需积分: 16 20 浏览量
2009-03-24
23:19:24
上传
评论 3
收藏 976B RAR 举报
lwqok
- 粉丝: 0
- 资源: 4
最新资源
- IMG_20240423_191216.jpg
- 基于MATLAB实现分析黑体辐射光谱图时将通过迈克尔逊干涉仪得到干涉图经过傅里叶变换转换为光谱图的功能源码.zip
- DynamicArray02.java
- 数据可视化 matplotlib 实例.docx
- 数据可视化 matplotlib 实例.7z
- Python 使用 Matplotlib 绘制甘特图的实践.7z
- Python 使用 Matplotlib 绘制甘特图的实践.docx
- matplotlib 中的 cmap 参数介绍.docx
- matplotlib 中的 cmap 参数介绍.7z
- 基于matlab的毕业设计基于Matlab使用LQR实现车辆轨迹跟踪源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈