#include "fat.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ReserveBlock sys1;
struct FCBBlock *fcb;
struct DirBlock fil[32],*dir;//目录项
int *fat1;
char *str,*ptr;
char fillchar[BlockSize];
FILE *fp;
/*a、 打开文件系统 */
FILE *OPENSYS(char *filename)
{ int i;
fp=fopen(filename,"rb+");
fread(&sys1,1,BlockSize,fp);
//printf("%d %d %d %d \n",sys1.sysblocknum,sys1.resblocknum,sys1.fatblocknum,sys1.rootblocknum);
fat1=(int *)malloc(sys1.sysblocknum);
for(i=0;i<sys1.fatblocknum;i++)
fread(fat1,sizeof(int)*sys1.sysblocknum,1,fp);//把基本的文件系统都读进来
fseek(fp,(sys1.fatblocknum+sys1.resblocknum)*BlockSize,0);//修改文件指针的位置
dir=fil;//目录指针
fread(dir,DirSize*32,1,fp);
return fp;
}
/*b、 关闭文件系统 */
int CLOSESYS(FILE *stream)
{ int i;
fseek(stream,sys1.resblocknum*BlockSize,0);
fwrite(fat1,sizeof(int)*sys1.sysblocknum,1,stream);
fseek(fp,(sys1.fatblocknum+sys1.resblocknum)*BlockSize,0);
fwrite(dir,DirSize*32,1,fp);
fclose(fp);
return 1;
}
/*c、 显示目录 */
void LISTDIR(void)
{
int i,flag=0;
for(i=0;i<32;i++)
{
if(fil[i].firstblockaddr!=0)
{
if(flag==0)
printf("filename \n");
flag=1; //标示
printf("%s\n",fil[i].filename);
}
}
}
/*d、 建立文件 */
int FCREATE(char *filename)
{
int i,flag=0,j,k=0,flag1=0,flag2=0,a;
int n,m;
while(1)
{a=strlen(filename);//文件名长度
if(a>10)
{printf("zhe file length too long!\n");
printf("input file name again:");
scanf("%s",filename);
}
else
break;
}
while(1)
{
for(i=0;i<32;i++)
if(strcmp(filename,fil[i].filename)==0)
{
printf("the name already exist \n");
printf("input name again:");
flag1=1;
break;
}
if(flag1==0)
break;
scanf("%s",filename);
flag1=0;
}
for(i=(sys1.fatblocknum+sys1.resblocknum+sys1.rootblocknum);i<sys1.sysblocknum;i++)
if(fat1[i]==0)
flag++;//统计磁盘上为空数目
if(flag==0)
{ printf("ci pan yi man");//统计结果为0,则磁盘已满
return 0;
}
printf("kong xian kuai shu:%d\n",flag);
printf("input file length:");
for(j=0;j<32;j++)
if(fil[j].firstblockaddr==0)
break;
while(1)
{scanf("%d",&dir[j].filelen);//输入目录项中文件的长度
n=(dir[j].filelen/BlockSize)+(dir[j].filelen%BlockSize?1:0);
if(n<0||n>flag)//文件长度小于0或大于空闲的空间
{printf("input length too long!\n");
printf("input again:");
}
else
break;
}
for(i=(sys1.fatblocknum+sys1.resblocknum+sys1.rootblocknum);i<sys1.sysblocknum;i++)//文件内容区域
{ if(fat1[i]==0)
{ k++;
if(flag2==0)
{ dir[j].firstblockaddr=i+1;
flag2=1;
}
else
{ if(k>=n)//n 文件长度
{ fat1[m]=i+1;
fat1[i]=-1;
break;
}
else
fat1[m]=i+1;
}
m=i;
}
}
strcpy(dir[j].filename,filename);
dir[j].filename[a]='\0';
printf("input year:");
scanf("%d",&dir[j].year);
printf("input month:");
scanf("%d",&dir[j].month);
printf("input day:");
scanf("%d",&dir[j].day);
return (1);
}
/*e、 删除文件 */
int FDELETE(char *filename)
{int i,j,k,n,flag=0;
struct FCBBlock *p;//文件控制块指针
p=fcb;
while(p)
{
if(strcmp(filename,fil[p->fdtblockindex].filename)==0)//目录项所在内序号
{printf("the file has open,please close it before delete !\n");
return 0;
}
else
p=p->next;
}
for(i=0;i<32;i++)//查找要关闭的文件
if(strcmp(filename,fil[i].filename)==0)
{ flag=1;
break;
}
if(flag==0)
{printf("file cannot find!\n");
return 0;
}
j=fil[i].firstblockaddr;
while(1)
{
k=fat1[j-1];
fseek(fp,(j-1)*BlockSize,0);//将指针指向文件的第一个扇区的位置
fwrite(&fillchar,1,BlockSize,fp);
fat1[j-1]=0;
if(k==-1)
break;
else
j=k;
}
memset(&fil[i],0,DirSize);
return 1;
}
/*f、 打开文件 */
int FOPEN(char *filename)
{
int i,j=0,k,flag=0;
struct FCBBlock *p,*q,*r;
p=fcb;
for(i=0;i<32;i++)
if(strcmp(filename,fil[i].filename)==0)//找到要打开的文件
{ flag=1;
break;
}
if(flag==0)
{printf("file cannot find!\n");
return 0;
}
while(p)
{j++;
if(strcmp(filename,fil[p->fdtblockindex].filename)==0)
{
printf("zhe file has open!\n");
return 0;
}
q=p;
p=p->next;
}
if(fcb==NULL)
{ fcb=(struct FCBBlock *)malloc(sizeof(struct FCBBlock));
fcb->fileid=j+1;//文件标示
fcb->filepos=0;//文件读写指针
fcb->next=NULL;//指向下一个文件控制指针
fcb->fdtblockindex=i;
}
else
{ r=(struct FCBBlock *)malloc(sizeof(struct FCBBlock));
r->fileid=j+1;
r->filepos=0;
r->next=NULL;
r->fdtblockindex=i;
q->next=r;
}
//f->next=NULL;
return 1;
}
long FGETLEN(int fileid)//获取文件指针
{ struct FCBBlock *p;
p=fcb;
while(p)
{if(p->fileid==fileid)//寻找要找的指针
return(p->filepos);
p=p->next;
}
printf("get faile!\n");
return(0);
}
/*g、 关闭文件 */
int FCLOSE(char *filename)
{ int flag=0;
struct FCBBlock *f,*p;
f=fcb;
if(strcmp(filename,fil[fcb->fdtblockindex].filename)==0)
{ flag=1;
fcb=fcb->next;
p=fcb;
while(p)
{p->fileid-=1;
p=p->next;
}
return flag;
}
p=f;
f=f->next;
while(f)
{
if(strcmp(filename,fil[f->fdtblockindex].filename)==0)
{ flag=1;
p=f->next;
//p=p->next;
while(p)
{ p=p->next;
p->fileid-=1;
}
free(f);
return flag;
}
p=f;
f=f->next;
}
if(flag==0)
{ printf("zhe file has not open!\n'");
return flag;
}
return 0;
}
/*h、 文件块读 */
int FREAD(char *str, int n, int fileid)
{ int m,i,j=0,k,l,len;
char a[3];
struct FCBBlock *p;
p=fcb;
while(p)
{if(p->fileid==fileid)
break;
else
p=p->next;
}
len=fil[p->fdtblockindex].filelen;//文件长度
l=p->filepos;//文件读写指针:该块在文件的相对位置
printf("now file pos is:");
printf("%d\n",l);
printf("is writing as now file pos?(y/n):");
scanf("%s",a);
if(a[0]=='n'||a[0]=='N')
{printf("input pos:");
while(1)
{scanf("%d",&l);
if(fil[p->fdtblockindex].filelen<(l+n))//?
printf("too large!\ninput again:");
else
break;
}
}
while(1)
{
if(n>fil[p->fdtblockindex].filelen)
{printf("too large!\n");
printf("input read size,again:");
scanf("%d",&n);
}
else
break;
}
str=(char *)malloc(fil[p->fdtblockindex].filelen+1);
m=(len/BlockSize)+(len%BlockSize?1:0);
k=fil[p->fdtblockindex].firstblockaddr;
if(m>1)
{ fseek(fp,(k-1)*BlockSize,0);
fread(str,BlockSize,1,fp);
k=fat1[k-1];
for(i=1;i<(m-1);i++)
{fseek(fp,fat1[k-1]*BlockSize,0);
fread(&str[i*BlockSize],BlockSize,1,fp);
k=fat1[k-1];
}
fseek(fp,(k-1)*BlockSize,0);
fread(&str[i*BlockSize],len-i*BlockSize,1,fp);
}
else
{ fseek(fp,(k-1)*BlockSize,0);
fread(str,len,1,fp);
}
str[n+l]='\0';
if(l!=0)
str=&str[l];
p->filepos=n+l;
printf("%s\n",str);
return 1;
}
/*i、 文件块写 */
void FWRITE(char *ptr, int n, int fileid)
{ char a[3];
long l;
int m,i=0,j,k=0,num,flag=0,flag1=0,b;
struct FCBBlock *p;
p=fcb;
while(p)
{if(p->fileid==fileid)
break;
else
p=p->next;
}
l=p->filepos;
printf("now file pos is:");
printf("%d\n",l);
printf("is writing as now file pos?(y/n):");
scanf("%s",a);
if(a[0]=='n'||a[0]=='N')
{printf("input pos:");
while(1)
{scanf("%d",&l);
if(fil[p->fdtblockindex].filelen<(l+n))
printf("too large!\ninput again:");
else
break;
}
}
j=(l/BlockSize)+(l%BlockSize?1:0);
if(l==0)
j=1;
num=fil[p->fdtblockindex].firstblockaddr;
for(i=1;i<j;i++)
num=fat1[num];
do
{
if(flag==0)
{fseek(fp,(num-1)*BlockSize+l-(i-1)*BlockSize,0);
flag=1;
}
else
没有合适的资源?快使用搜索试试~ 我知道了~
操作系统FAT文件系统源程序
共24个文件
pdb:3个
ncb:2个
exe:2个
需积分: 10 28 下载量 163 浏览量
2010-03-18
10:05:37
上传
评论 3
收藏 393KB RAR 举报
温馨提示
操作系统课程设计,用VC++环境编写,包括FAT文件系统对文件操作的各项功能
资源推荐
资源详情
资源评论
收起资源包目录
fat.rar (24个子文件)
fat
fatfmt.dsp 3KB
fatfmt.plg 1KB
fat.h 915B
fatfmt.opt 48KB
fat.opt 48KB
fatfmt.ncb 41KB
fat.dsp 4KB
fat.plg 2KB
Debug
fat.pdb 393KB
vc60.pdb 52KB
vc60.idb 41KB
fat.exe 196KB
fatfmt.exe 196KB
fat.ilk 203KB
fatfmt.obj 33KB
fat.pch 223KB
fatfmt.pdb 393KB
fatfmt.pch 223KB
fatfmt.ilk 202KB
fatfmt.cpp 12KB
fat.ncb 33KB
fatsys.dat 4.88MB
fat.dsw 514B
fatfmt.dsw 518B
共 24 条
- 1
资源评论
yitiantian131
- 粉丝: 0
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功