工控编程吧
标题:
文件操作例程,纯C++ 编写
[打印本页]
作者:
qq263946146
时间:
2019-8-22 10:10
标题:
文件操作例程,纯C++ 编写
(, 下载次数: 0)
上传
点击文件名下载附件
可以看到程序运行的界面。
DOC环境运行,通过输入数值执行对应功能。
别人写的,就两个文件,内容如下。
可以参考使用。
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include <string.h>
#define false 0
#define true 1
#define n 5 /*模拟实验中系统允许打开文件的最大数量*/
class Content /*目录结构*/
{
public:
char name[3]; /*文件或目录名*/
char type[3]; /*文件类型名*/
char attribute; /*属性*/
char address; /*文件或目录的起始盘块号*/
char length; /*文件长度,以盘块为单位*/
};
/*已打开文件表中读写指针的结构*/
class Pointer
{
public:
int dnum; /*磁盘盘块号*/
int bnum; /*盘块内第几项*/
};
/*已打开文件表项类型定义*/
class OFILE
{
public:
char name[20]; /*文件绝对路径名*/
char attribute;/*文件的属性,用1个字节表示,所以用了char类型*/
int number; /*文件起始盘块号*/
int length; /*文件长度,文件占用的字节数*/
int flag; /*操作类型,用"0"表示以读操作方式开文件,用"1"表示写操作方式打开文件*/
Pointer read; /*读文件的位置,文件刚打开时dnum为文件起始盘块号,bnum为"0"*/
Pointer write; /*写文件的位置,文件建立时dnum为文件起始盘块号,bnum为"0",打开时为文件末尾*/
};
/*已打开文件表定义*/
class OpenFile
{
public:
OFILE file[n]; /*已打开文件表*/
int length; /*已打开文件表中登记的文件数量*/
};
char buffer1[64];/*模拟缓冲1*/
Content buffer2[8];/*模拟缓冲2*/
FILE *fc; /*模拟磁盘的文件指针*/
复制代码
#include "FileSystem.h"
Content content;
Pointer ponter;
OFILE ofile;
OpenFile openfile;
//文件操作
/*查找路径名为name的文件或目录,返回该目录的起始盘块号 */
int search(char name[],int flag,int *dnum,int *bnum)/*flag=8表示查找目录,否则为文件*//*返回找到文件或目录的目录项的位置:盘块dnum中第bnum项*/
{
int k,i,s,j,last=0;
char pna[3],type[3];
if((strcmp(name,"")==0)||(strcmp(name,"/")==0))/*根目录*/
return(2);
k=0;
if(name[0]=='/')k=1;
i=2; /*i=根目录的起始盘块号*/
while(last!=1)
{
/*pna=从name中分离出"/"后一个目录名(或文件名)*/
for(s=0;name[k]!='.'&&name[k]!='/'&&s<3&&name[k]!='\0';s++,k++)
pna[s]=name[k];
for(;s<3;s++)/*用空格补全名字长度*/
pna[s]=' ';
while(name[k]!='.'&&name[k]!='\0'&&name[k]!='/')/*除去多余字符*/
k++;
type[0]=type[1]=' ';
if(name[k]=='.')/*取文件类型名type*/
if(flag==8)
{
printf("Dir shouldn't contain file type,failed!\n\n");
return(false);
}
else
{/*文件遇到类型名认为结束,后面的字符作废*/
k++;
if(name[k]!='\0')
type[0]=name[k];
k++;
if(name[k]!='\0')
type[1]=name[k];
k++;
if(name[k]!='\0')
type[2]=name[k];
if(name[k]!='\0'&&name[k+1]!='\0')
{
printf("Wrong file name!\n\n");
return(false);
}
last=1;
}
else
if(name[k]!='\0')
k++;
if(name[k]=='\0')
last=1;
/*查找目录且名字等于pna的目录项*/
fseek(fc,i*64L,SEEK_SET);
fread(buffer2,64L,1,fc);
j=0;
if(last==1&&flag!=8)
while(j<8&&!(buffer2[j].attribute!=8&&buffer2[j].name[0]==pna[0]&&
buffer2[j].name[1]==pna[1]&&buffer2[j].name[2]==pna[2]&&
buffer2[j].type[0]==type[0]&&buffer2[j].type[1]==type[1]&&buffer2[j].type[2]==type[2]))
j++;
else
while(j<8&&!(buffer2[j].attribute==8&&buffer2[j].name[0]==pna[0]&&buffer2[j].name[1]==pna[1]
&&buffer2[j].name[2]==pna[2]))
j++;
if(j<8)/*找到该目录或文件*/
if(last==1)/*查找结束*/
{
*dnum=i;
*bnum=j;
return(buffer2[j].address);
}
else/*查找还未结束*/
i=buffer2[j].address;/*读取下一个盘块*/
else
return(false);
}/*while 查找结束*/
return 1;
}/*search()结束*/
/*分配一个磁盘块,返回块号*/
int allocate( )
{
int i;
fseek(fc,0,SEEK_SET); /*将模拟磁盘的文件指针移至模拟磁盘FAT表*/
fread(buffer1,64L,1,fc);/*将FAT表中第一个磁盘块读入模拟缓冲buffer1中*/
for(i=3;i<63;i++)
if(buffer1[i]==0)
{ /*FAT中的第i项为0,分配第i块磁盘块,修改FAT表,并且写回磁盘*/
buffer1[i]=255;
fseek(fc,0,SEEK_SET);
fwrite (buffer1,64L,1,fc);
return(i); /*返回磁盘号*/
}
fread(buffer1,64L,1,fc);/*将FAT表中第二个磁盘块读入模拟缓冲buffer1中*/
for(i=0;i<63;i++)
if(buffer1[i]==0)
{/*FAT中的第i项为0,分配第i+64块磁盘块,修改FAT表,并且写回磁盘*/
buffer1[i]=255;
fseek(fc,-64L,SEEK_CUR);
fwrite(buffer1,64L,1,fc);
return(i+64); /*返回磁盘号*/
}
printf("Disk full,please clear file system!\n\n");
return(false);
}
/*分配磁盘块函数结束*/
void copen(OFILE *x1,OFILE *x2)
{
strcpy(x1->name,x2->name);
x1->attribute=x2->attribute;
x1->number=x2->number;
x1->length=x2->length;
x1->flag=x2->flag;
x1->read.dnum=x2->read.dnum;
x1->read.bnum=x2->read.bnum;
x1->write.dnum=x2->write.dnum;
x1->write.bnum=x2->write.bnum;
}
/*在已打开文件表中查找文件name*/
int sopen(char name[])
{
int i;
i=0;
while(i<openfile.length&&strcmp(openfile.file[i].name,name)!=0)/*依次查找已打开文件表*/
i++;
if(i>=openfile.length)
return(-1);
return(i);
}/*查找sopen函数结束*/
void dopen(char name[]) /*在已打开文件表中删除文件name*/
{
int i;
i=sopen(name);
if(i==-1)
printf("You need to open that file first!\n\n");
else
{
copen(&openfile.file[i],&openfile.file[openfile.length-1]);
openfile.length--;
}
}/*删除函数结束*/
/*在已打开文件表中插入文件name*/
int iopen(OFILE *x)
{
int i;
i=sopen(x->name);
if(i!=-1)
{
printf("File is already open!\n\n");
return(false);
}
else if(openfile.length==n)
{
printf("Table of open file is full!\n\n");
return(false);
}
else
{
copen(&openfile.file[openfile.length],x);
openfile.length++;
return(true);
}
}
/*填写已打开文件表函数结束*/
/*建立文件函数,路径名name,文件属性attribute*/
int create_file(char name[])
{
int i,j,k,s,d,t,b,dd,dn,bn;
int attribute=4;
char dname[3],tname[3],pathname[20];
OFILE x;
if(attribute%2==1)
{
printf("Read only,can't write!\n\n");
return(false);
}
if(openfile.length==n)
{
printf("Table of open file is full,can't creat!\n\n");
return(false);
}
/* 将name分成两部分,目录路径pathname和目录名dname*/
for(j=0;name[j]!='\0';j++) /*查找最后一个“/”*/
if(name[j]=='/')
s=j;
/*分离目录路径*/
for(j=0;j<s;j++)
pathname[j]=name[j];
pathname[j]='\0';
/*分离文件名*/
for(k=0,j=s+1;name[j]!='\0'&&k<3&&name[j]!='.';j++,k++)
dname[k]=name[j];
if(k==0)
{
printf("Wrong file name or dir!\n\n");
return(false);
}
for(;k<3;k++)
dname[k]=' ';
k=0;
if(name[j++]=='.')/*分离类型名*/
{
for(;name[j]!='\0'&&k<5&&name[j]!='.';j++,k++)
tname[k]=name[j];
}
for(;k<3;k++)
tname[k]=' ';
if((d=search(pathname,8,&dn,&bn))==false)/*找到目录路径,返回该目录所在块号dn和项数bn*/
{
printf("Dir isn't exist,can't creat!\n\n");
return(false);
}
/*确认该目录不存在的同时查找空目录项*/
b=-1;
fseek(fc,d*64L,SEEK_SET);
fread(buffer2,64L,1,fc); /*读出dnum盘块的内容*/
for(t=0;t<8;t++)
{
if(buffer2[t].name[0]==dname[0]&&buffer2[t].name[1]==dname[1]&&buffer2[t].name[2]==dname[2]
&&buffer2[t].type[0]==tname[0]&&buffer2[t].type[1]==tname[1]&&buffer2[t].type[2]==tname[2])
{ /*找到名字dname的文件,建立失败*/
printf("Already exist,can't creat!\n\n");
return(false);
}
if(buffer2[t].name[0]=='
(, 下载次数: 0)
上传
点击文件名下载附件
&&b==-1)
b=t;
}/*for*/
if(b==-1)/*没有空目录项,建立失败*/
{
printf("Disk full,you should first clear file system!\n\n");
return(false);
}
if((dd=allocate( ))==false)/*分配给建立目录的磁盘盘块dd*/
{
printf("Creat failed!\n\n");
return(false);
}
/*填写目录项*/
for(i=0;i<3;i++)
buffer2[b].name[i]=dname[i];
for(i=0;i<3;i++)
buffer2[b].type[i]=tname[i];
buffer2[b].attribute=attribute;
buffer2[b].address=dd;
buffer2[b].length=0;
fseek(fc,d*64L,SEEK_SET);
fwrite(buffer2,64L,1,fc);
/*填写已打开文件表*/
strcpy(x.name,name);
x.attribute=attribute;
x.number=dd;
x.length=0;
x.flag=1;
x.read.dnum=x.write.dnum=dd;
x.read.bnum=x.write.bnum=0;
printf("Creat file successfully!\n\n");
iopen(&x);
return (true);
}/*建立文件结束*/
/*打开文件函数*/
int open_file(char name[],int attribute)
{
OFILE x;
int dnum,bnum,last,i,d;
if((d=search(name,4,&dnum,&bnum))==false)
{
printf("File isn't exist,open failed!\n");
return(false);
}
fseek(fc,dnum*64L,SEEK_SET);/*读出对应目录项*/
fread(buffer2,64,1,fc);
if((buffer2[bnum].attribute%2==1)&&
attribute==1)/*对只读文件要求写*/
{
printf("Read only,open failed!\n\n");
return(false);
}
strcpy(x.name,name);
x.attribute=buffer2[bnum].attribute;
x.number=buffer2[bnum].address;
x.read.dnum=x.write.dnum=buffer2[bnum].address;
x.read.bnum=x.write.bnum=0;
x.flag=attribute;
if(attribute==1)
{
while(d!='\xff')/*寻找文件末尾*/
{
fseek(fc, d/64*64L, SEEK_SET);
fread(buffer1,64L,1,fc);/*读出dnum项所在FAT*/
last=d;
d=buffer1[d%64];/*读出dnum块下一块内容赋给dnum*/
}/*while*/
x.write.dnum=last;/*填写写指针*/
fseek(fc, last*64L, SEEK_SET);
fread(buffer1,64L,1,fc);
for(i=0;i<64&&buffer1[i]!='#';i++);
x.write.bnum=i;
x.length=(buffer2[bnum].length-1)*64+i;
}
iopen(&x);/*填写已打开文件表*/
printf("File opened successfully!\n\n");
return 1;
}
/*文件打开结束*/
/*关闭文件函数*/
int close_file(char name[])
{
int i,dnum,bnum;
if((i=sopen(name))==-1)
{
printf("You need first open it,close failed!\n\n");
return(false);
}
if(openfile.file[i].flag==1)/*写文件的追加文件结束符*/
{
fseek(fc,openfile.file[i].write.dnum*64L, SEEK_SET);
fread(buffer1,64,1,fc);
buffer1[openfile.file[i].write.bnum]='#';
fseek(fc,openfile.file[i].write.dnum*64L, SEEK_SET);
fwrite(buffer1,64,1,fc);
fputc('#',fc);
search(name,4,&dnum,&bnum);/*查找该文件目录位置*/
/*修改目录中的文件长度*/
fseek(fc,dnum*64L, SEEK_SET);
fread(buffer2,64,1,fc);
buffer2[bnum].length=openfile.file[i].length/64+1;
fseek(fc, dnum*64L, SEEK_SET);
fwrite(buffer2,64,1,fc);
}
/*在已打开文件表中删除该文件的登记项*/
if(openfile.length>1)
copen(&openfile.file[i],&openfile.file[openfile.length-1]);
openfile.length--;
printf("File closed successfully!\n\n");
return 1;
}
/*读文件函数,文件路径名name,读取长度length*/
int read_file(char name[])
{
int i,t;
int length=20;
if((i=sopen(name))==-1)
{
printf("You need to open it first or it isn't exist!\n\n");
return(false);
}
if(openfile.file[i].flag==1)
{
printf("Open in write mode! Please close it and open it in read mode!\n\n");
return(false);
}
t=0;
fseek(fc,openfile.file[i].read.dnum*64L,SEEK_SET);
fread(buffer1,64,1,fc);
while(t<length&&buffer1[openfile.file[i].read.bnum]!='#')
{
putchar(buffer1[openfile.file[i].read.bnum]);/*读出一个字符(这里是在屏幕上显示)*/
if((t+1)%64==0)putchar('\n');
/*修改读指针*/
openfile.file[i].read.bnum++;
if(openfile.file[i].read.bnum>=64)/*一块读完,读取下一个盘块*/
{
fseek(fc,openfile.file[i].read.dnum/64*64, SEEK_SET);
fread(buffer1,64,1,fc);
openfile.file[i].read.dnum=buffer1[openfile.file[i].read.dnum%64];/*修改读指针*/
openfile.file[i].read.bnum=0;
fseek(fc,openfile.file[i].read.dnum*64L,SEEK_SET);
fread(buffer1,64,1,fc);/*读取下一个*/
}
t++;
}
printf("\n");
return 1;
}
/*读函数结束*/
/*写文件函数*/
int write_file(char name[],char buff[],int length)/*文件路径名 存放准备写入磁盘的内容 写入内容的长度*/
{
int i,t,dd;
int d,dn,bn;
if((i=sopen(name))==-1)/*文件不存在,无法写*/
{
printf("You need to open it first or it isn't exist!\n\n");
return(false);
}
if(openfile.file[i].flag==0)
{
printf("Open in read mode,can't write!\n\n");
return(false);
}
t=0;
fseek(fc,openfile.file[i].write.dnum*64L, SEEK_SET);
fread(buffer1,64,1,fc);
while(t<length)
{
buffer1[openfile.file[i].write.bnum]=buff[t];
openfile.file[i].write.bnum++;
openfile.file[i].length++;
if(openfile.file[i].write.bnum>=64)
{
fseek(fc, openfile.file[i].write.dnum*64L, SEEK_SET);
fwrite(buffer1,64,1,fc);/*一块写完,写回磁盘*/
if((dd=allocate())==false)
{
openfile.file[i].write.bnum--;
openfile.file[i].length--;
printf("Disk full,some info missing,failed!\n\n");
return(false);
}/*if*/
fseek(fc,openfile.file[i].write.dnum/64*64L, SEEK_SET);
fread(buffer1,64,1,fc);
buffer1[openfile.file[i].write.dnum%64]=dd;
fseek(fc,openfile.file[i].write.dnum/64*64L, SEEK_SET);
fwrite(buffer1,64,1,fc);
openfile.file[i].write.dnum=dd;
openfile.file[i].write.bnum=0;
}/*if*/
t++;
}/*while*/
fseek(fc, openfile.file[i].write.dnum*64L, SEEK_SET);
fwrite(buffer1,64,1,fc);/*一块写完,写回磁盘*/
if((d=search(name,4,&dn,&bn))==false)/*找到目录路径,返回该目录所在块号dn和项数bn*/
{
printf("Tis dir isn't exist,can't creat!\n\n");
return(false);
}
fseek(fc,dn*64L,SEEK_SET);
fread(buffer2,64L,1,fc); /*读出dnum盘块的内容*/
buffer2[bn].length=length;
fseek(fc,dn*64L,SEEK_SET);
fwrite(buffer2,64L,1,fc);
printf("Content write successfully!\n\n");
return 1;
}/*写函数结束*/
/*删除文件*/
int delete_file(char name[])
{
int dnum,bnum,t;
if((t=search(name,4,&dnum,&bnum))==false)
{
printf("File isn't exist,failed!\n\n");
return(false);
}
if(sopen(name)!=-1)
{
printf("Please close it first,can't delete!\n\n");
return(false);
}
fseek(fc,dnum*64L, SEEK_SET);
fread(buffer2,64,1,fc);
buffer2[bnum].name[0]='
;/*将该文件的目录置成空目录*/
fseek(fc,dnum*64L, SEEK_SET);
fwrite(buffer2,64,1,fc);
while(t!='\xff')/*通过FAT查找每一个盘块号,并依次删除*/
{
dnum=t;
fseek(fc, dnum/64*64, SEEK_SET);
fread(buffer1,64,1,fc);
t=buffer1[dnum%64];
buffer1[dnum%64]=0;
fseek(fc, dnum/64*64L, SEEK_SET);
fwrite(buffer1,64,1,fc);
}
printf("File deleted successfuly!\n\n");
return 1;
}/*文件删除结束*/
/*建立目录函数,目录路径名name*/
int md(char name[])
{
int i,j,k,s,d,t,b,dd,dn,bn;
char dname[3],pathname[20];
i=2;/* i=根目录的起始盘块号*/
/* 将name分成两部分,目录路径pathname和目录名dname*/
for(j=0;name[j]!='\0';j++)/*查找最后一个“/”*/
if(name[j]=='/')s=j;
/*分离目录路径*/
for(j=0;j<s;j++)
pathname[j]=name[j];
pathname[j]='\0';
/*分离目录名*/
for(k=0,j=s+1;name[j]!='\0'&&k<3&&name[j]!='.';j++,k++)
dname[k]=name[j];
if(k==0)
{
printf("Wrong file name or dir!\n\n");
return(false);
}
for(;k<3;k++)
dname[k]=' ';
if((d=search(pathname,8,&dn,&bn))==false)/*找到目录路径*/
{
printf("Dir isn't exist,creat failed!\n\n");
return(false);
}
b=-1;
/*确认该目录不存在的同时查找空目录项*/
fseek(fc,d*64L,SEEK_SET);
fread(buffer2,64L,1,fc);/*读出d盘块的内容*/
for(t=0;t<8;t++)
{
if(buffer2[t].name[0]==dname[0]&&buffer2[t].name[1]==dname[1]
&&buffer2[t].name[2]==dname[2]&&buffer2[t].attribute==8)
{/*找到名字dname的目录,建立失败*/
printf("Already exist,can't creat!\n\n");
return(false);
}
if(buffer2[t].name[0]=='
&&b==-1)
b=t;
}/*for*/
if(b==-1)/*没有空目录项, 不能建立*/
{
printf("Dir item full!\n\n");
return(false);
}
if((dd=allocate( ))==false)/*分配给建立目录的磁盘盘块dd*/
{
printf("Dir can't creat!\n\n");
return(false);
}
/*填写目录项*/
for(i=0;i<3;i++)
buffer2[b].name[i]=dname[i];
buffer2[b].type[0]='d';
buffer2[b].type[1]='i';
buffer2[b].type[2]='r';
buffer2[b].attribute=8;
buffer2[b].address=dd;
buffer2[b].length=0;
fseek(fc,d*64L,SEEK_SET);
fwrite(buffer2,64L,1,fc);
/*分给新建目录的盘块初始化*/
for(t=0;t<8;t++)
buffer2[t].name[0]='
;
fseek(fc, dd*64L, SEEK_SET);
fwrite(buffer2,64L,1,fc);
printf("Dir created successfully!\n\n");
return 1;
}
/*建立目录结束*/
/*删除目录函数,目录路径名name*/
int rd(char name[])
{
int i,j,k,s,d,t,b,dn,bn;
char dname[3],pathname[20];
i=2;/* i=根目录的起始盘块号*/
/* 将name分成两部分,目录路径pathname和目录名dname*/
for(j=0;name[j]!='\0';j++)/*查找最后一个“/”*/
if(name[j]=='/')s=j;
/*分离目录路径*/
for(j=0;j<s;j++)
pathname[j]=name[j];
pathname[j]='\0';
/*分离目录名*/
for(k=0,j=s+1;name[j]!='\0'&&k<3&&name[j]!='.';j++,k++)
dname[k]=name[j];
if(k==0)
{
printf("Wrong dir name!\n\n");
return(false);
}
for(;k<3;k++)
dname[k]=' ';
if((d=search(pathname,8,&dn,&bn))==false)/*找到目录路径*/
{
printf("Aleady exist,can't creat!\n");
return(false);
}
b=-1;
/*确认该目录不存在的同时查找空目录项*/
fseek(fc,d*64L,SEEK_SET);
fread(buffer2,64L,1,fc);/*读出d盘块的内容*/
for(t=0;t<8;t++)
{
if(buffer2[t].name[0]==dname[0]&&buffer2[t].name[1]==dname[1]
&&buffer2[t].name[2]==dname[2]&&buffer2[t].attribute==8)
{
b=t;
}
}
/*填写目录项*/
buffer2[b].name[0]='
;
fseek(fc,d*64L,SEEK_SET);
fwrite(buffer2,64L,1,fc);
printf("Dir deleted successfully!\n\n");
return 1;
}
/*删除目录结束*/
/*显示目录内容*/
int dir(char name[])
{
int dnum,t,dn,bn;
if((dnum=search(name,8,&dn,&bn))==false)/*找到目录路径,返回该目录所在块号dn和盘块内项数bn*/
{
printf("Dir isn't exist!\n\n");
return(false);
}
printf("Name Type Disk Length\n");
/*显示目录内容*/
fseek(fc,dnum*64L,SEEK_SET);
fread(buffer2,64L,1,fc);
for(t=0;t<8;t++)/*显示该盘块中目录项的内容*/
if(buffer2[t].name[0]!='
)
printf("%c%c%c %c%c%c %4d %7d\n", buffer2[t].name[0],
buffer2[t].name[1],
buffer2[t].name[2], buffer2[t].type[0],
buffer2[t].type[1],buffer2[t].type[2], buffer2[t].address,
buffer2[t].length);
printf("\n");
return 1;
}
/*显示目录函数结束*/
/*显示文件内容*/
int typefile(char name[])
{
int dnum,dn,bn,t;
if((dnum=search(name,1,&dn,&bn))==false)
{
printf("File is exist!\n\n");
return(false);
}
if(sopen(name)!=-1)
{
printf("File is open,please close it first!\n\n");
return(false);
}
while(dnum!='\xff')
{
fseek(fc,dnum*64L,SEEK_SET);
fread(buffer1,64,1,fc);/*读一个盘块到缓冲*/
for(t=0;t<64&&buffer1[t]!='#';t++)/*显示缓冲中内容*/
putchar(buffer1[t]);
printf("\n");
/*获得下一个盘块*/
fseek(fc, dnum/64*64L, SEEK_SET);
fread(buffer1,64,1,fc);
dnum=buffer1[dnum%64];
}
printf("\n");
return 1;
}
/*显示文件函数结束*/
int change(char name[],int attribute)
/*改变文件name的属性为attribute*/
{
int dnum,bnum;
if(search(name,1,&dnum,&bnum)==false)/*查找文件目录*/
{
printf("File isn't exist!\n\n");
return(false);
}
if(sopen(name)!=-1)
{
printf("Please close it first,change failed!\n\n");
return(false);
}
fseek(fc,dnum*64L,SEEK_SET);
fread(buffer2,64,1,fc);/*读出该目录所在盘块*/
buffer2[bnum].attribute=attribute;/*修改属性*/
fseek(fc,dnum*64L,SEEK_SET);
fwrite(buffer2,64,1,fc);/*写回磁盘*/
printf("File changed successfully!\n\n");
return 1;
}/*改变文件属性函数结束*/
void clear_fs()
{
/*建立文件,模拟磁盘*/
/*初始化已打开文件表*/
/*初始化磁盘*/
/*初始化文件分配表*/
int i;
if((fc=fopen("c:\\c.txt","w+"))==NULL)
{
printf("Can't open file!\n\n");
exit(0);
}
buffer1[0]=buffer1[1]=buffer1[2]=255;/*磁盘第0、1块存放FAT表,第2块存放根目录*/
for(i=3;i<64;i++)
buffer1[i]=0;
fwrite(buffer1,64L,1,fc);
for(i=0;i<64;i++)
buffer1[i]=0;
fwrite(buffer1,64L,1,fc);
/*初始化根目录*/
for(i=0;i<8;i++)
buffer2[i].name[0]='
;/*若目录项的第一个字符为“$”表示该目录项为空*/
fwrite(buffer2,64L,1,fc);
/*初始化已打开文件表*/
}
void show_menu()
{
printf(" File System \n");
printf("*************************************");
printf("\n");
printf("* 1 - Creat File *");
printf("\n");
printf("* 2 - Open File *");
printf("\n");
printf("* 3 - Read File *");
printf("\n");
printf("* 4 - Write File *");
printf("\n");
printf("* 5 - Close File *");
printf("\n");
printf("* 6 - Delete File *");
printf("\n");
printf("* 7 - Creat Dir *");
printf("\n");
printf("* 8 - Delete Dir *");
printf("\n");
printf("* 9 - Show Dir Content *");
printf("\n");
printf("* 10 - Show File Content *");
printf("\n");
printf("* 11 - Change File Type *");
printf("\n");
printf("* 12 - Clear File System *");
printf("\n");
printf("* 13 - Exit *");
printf("\n");
printf("*************************************\n");
printf("A kind remind: file name or type ,dir name is no more than three words!\n");
}
int main( )
{
char name[20];
int attribute,type,length,i,a,s;
char buffer[64];
if((fc=fopen("c:\\c.txt","r+"))==NULL)
{
clear_fs();
}
show_menu();
while(1)
{
printf("Choose Menu(1~13) \nChoose 0 is to show menu:");
scanf("%d",&a);
switch(a)
{
case 0: /*a=0程序结束*/
show_menu();
break;
case 1: /*a=1建立文件*/
printf("Please input the direction & file name \n(like /a.txt) :");
scanf("%s",name);
create_file(name); /*建立文件*/
break;
case 2: /*a=2打开文件*/
printf("Please input file name & type of open (0-Read,1-Write)\n(like /a.txt 1) :");
scanf("%s%d",name,&type);
open_file(name,type); /*打开文件*/
break;
case 3: /*a=3读文件*/
printf("Please input file name\n(like /a.txt) :");
scanf("%s",name);
read_file(name); /*读文件*/
break;
case 4: /*a=4写文件*/
printf("Please input file dir & file name \n(like /a.txt) :");
scanf("%s",name);
printf("Please input the content you want to add :\n");
scanf("%s",buffer);
s=strlen(buffer);
write_file(name,buffer,s); /*写文件*/
break;
case 5: /*a=5关闭文件*/
printf("Please input file dir & file name \n(like /a.txt) :");
scanf("%s",name);
close_file(name); /*关闭文件*/
break;
case 6: /*a=6删除文件*/
printf("Please input file dir & file name \n(like /a.txt) :");
scanf("%s",name);
delete_file(name); /*删除文件*/
break;
case 7: /*a=7建立目录*/
printf("Please input dir \n(like /abc) :");
scanf("%s",name);
md(name); /*建立目录*/
break;
case 8: /*a=8删除目录*/
printf("Please input dir \n(like /abc) :");
scanf("%s",name);
rd(name); /*显示目录*/
break;
case 9: /*a=9显示目录*/
printf("Please input dir \n(like /abc) :");
scanf("%s",name);
dir(name); /*显示目录*/
break;
case 10: /*a=10显示文件*/
printf("Please input file dir & file name \n(like /a.txt) :");
scanf("%s",name);
typefile(name); /*显示文件*/
break;
case 11:/* a=11改变文件属性 */
printf("Please input file name & type of file(1-Read only,4-Read & write)\n(like /a.txt 1) :");
scanf("%s%d",name,&attribute);
change(name,attribute);
break;
case 12:/* a=12 格式化 */
clear_fs();
printf("Clear file system successfully! \n\n");
break;
case 13: /*a=0程序结束*/
fclose(fc);
printf("Thank you for using my file system program!\nPress any key to quit.");
getchar();getchar();
exit(0);
}/* switch */
}/* while */
return 1;
}/*main( )结束*/
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4