磁盘文件的存储结构


/*************************************************************
/*编程时间:2015.6.4
/*程序目的:模拟设计MS-DOS操作系统中磁盘文件的存储结构。
/*************************************************************/
#include
#include
#include
#include
#define FDF -1
#define FFF -2
#define MAX1 10
#define MAX2 32
#define LIST_INIT_SIZE 200 //线性表存储空间的初始容量
#define LISTINCREMENT 20 //线性存储空间的分配增量


typedef struct
{
char name;
int start;
}fab;
fab a[MAX1];

typedef struct
{
int order; //索引表记录号
int block; //物理块号
}indextab;

typedef struct
{
char filename; //文件名
indextab *ind; //索引表指针
int length; //当前长度
int listsize; //当前分配的初始容量
}filetab;

typedef struct
{
filetab *file; //文件目录表指针
char name[10]; //用户名
int length; //当前长度
int listsize; //当前分配的初始容量
}usertab;
usertab *newbase;

typedef struct
{
usertab *user; //用户表指针
int length;
int listsize;
}Sqlist;
Sqlist s;



int fat[MAX2];//定义fat表项
int filenum = 1; //记录文件的个数,文件数随着保存而增加
int c = 0; //记录所有用户的数量
int h ; //记录每个用户文件的个数
int m ;//m存放的是文件的个数
int count1 = 0; //记录文件的个数,随着转换索引表而动态改变
char savename[10][1]; //保存用户名
char cr; //记录插入文件的名字



void begin();
void display();
void insert();
void save();
void change(char name[]);
void after();
void write(int n);
void read(int n);



void display()
{
int i = 0,n,q;
printf("此时fat表纪录情况为:\n");
for(i=0;iprintf("%d : %d\n",i,fat[i]);
printf("\n此时文件目录表中的情况为:\n");
printf("-------------------------------------------\n");
printf("文件名\t\t起始块号\t文件长度\n");
for(i=0;i<=filenum;i++)
{
q=0;
n=a[i].start;
while(1)
{
n = fat[n];
q++; //记录此文件的逻辑记录长度
if(n==FFF)
break;
}
printf("%4c\t\t%4d\t\t%4d\n",a[i].name,a[i].start,q);
}
printf("-------------------------------------------\n");
}


void begin()
{
int i,j = 0;
printf("初始化fat表\n");
fat[0] = FDF;
fat[1] = FFF;
for(i=2;ifat[i] = 0;

a[0].name = 'A';
a[0].start = 2;
fat[a[0].start] = 5; //初始化第一个文件
fat[5] = 8;
fat[8] = 11;
fat[11] = 18;
fat[18] = 24;
fat[24] = FFF;

a[1].name = 'B'; //初始化第二个文件
a[1].start = 6;
fat[a[1].start] = 9;
fat[9] = 14;
fat[14] = 19;
fat[19] = 22;
fat[22] = 28;
fat[28] = 30;
fat[30] = FFF;
display();
https://www.360docs.net/doc/bd1745587.html,er = (usertab *)malloc(LIST_INIT_SIZE*sizeof(usertab)); //为用户分配

空间
if(!https://www.360docs.net/doc/bd1745587.html,er)
exit(-2);
s.length = 0;
s.listsize = LIST_INIT_SIZE;
}

void insert()
{
int k,j,start1,s,n,q,w;
int i=0,p=0;
char iname,jname;
printf("请输入你要插入的文件名\n");
fflush(stdin);
scanf("%c",&iname);
for(i=0;i{
if(a[i].name!=iname)
{
q = i; //记录是第几个文件
continue;
}
else
{
jname = a[i].name;
cr = iname; //记录插入文件的名字
printf("存在此文件!\n");
n = a[i].start;
while(1)
{
n = fat[n];
p++; //记录此文件的逻辑记录长度
if(n==FFF)
break;
}
printf("此文件的记录长度为:%d\n",p);
while(1)
{
printf("请在此1--%d范围内输入你要插入的记录号\n",p);
scanf("%d",&k);
fflush(stdin);
if(k<=0||k>p)
{
printf("记录号错误重新输入\n");
continue;
}
else break;
}
for(i=0;i{
if(fat[i]!=0)
{
w = i;
continue;
}
else
{
printf("有空闲块%d\n",i);
j = i; //j为空闲块号
printf("逻辑纪录号对应的物理块号为:");
for(i=0;i{
if(iname==a[i].name)
{
start1=a[i].start;
break;
}
}
for(i=0;i{
m = fat[start1];
start1 = m;
}
printf("%d\n",start1);
s = fat[start1]; //插入纪录
fat[start1] = j;
fat[j] = s;
break;
}
if(w==MAX2-1&&fat[w]!=0)
{
printf("无空闲块结束\n");
break;
}
}
}
break;
}
if(q==MAX1-1&&jname!=iname)
printf("不存在此文件,结束\n");
}

void save()
{
char name1;
int num1,i,count=0;
int b[MAX2]; //数组b中存放空闲区物理块号
printf("请输入要保存文件的信息:文件名和长度\n");
fflush(stdin);
scanf("%c %d",&name1,&num1);
fflush(stdin);
filenum++;
m=filenum; //m存放的是文件的个数
for(i=0;i{
if(name1==a[i].name)
{
printf("有同名文件!\n");
return;
}
}
printf("无同名文件,可以保存!\n");
for(i=0;i{
if(fat[i]==0)
{
b[count] = i;
count++;
}
}
if(count{
printf("磁盘空间不够!\n");
return;
}
a[filenum].name = name1;
a[filenum].start = b[0];
printf("已将文件存入物理块中\n");
for(i=0;ifat[b[i]] = b[i+1];
fat[b[i]] = FFF;
}

void change(char name[])
{
int j=0; //记录当前用户文件的个数
int k,n,i;
int flag;
char fname;
if(s.length>=s.listsize)
{
newbase = (usertab *)realloc(https://www.360docs.net/doc/bd1745587.html,er,(s.listsize+LISTINCREMENT)*sizeof
(usertab));
if(!newbase)
exit(-2);
https://www.360docs.net/doc/bd1745587.html,er =

newbase;
s.length+=LISTINCREMENT;
}
for(i=0;i<10;i++) //将用户名记录到用户表中
https://www.360docs.net/doc/bd1745587.html,er[c].name[i] = name[i];
printf("用户的名字:%s\n",https://www.360docs.net/doc/bd1745587.html,er[c].name);
https://www.360docs.net/doc/bd1745587.html,er[c].file = (filetab *)malloc(LIST_INIT_SIZE*sizeof(filetab)); //为文件目录表分配空间
if(!https://www.360docs.net/doc/bd1745587.html,er[c].file)
exit(-2);
https://www.360docs.net/doc/bd1745587.html,er[c].length = 0;
s.length++; //用户个数加一
https://www.360docs.net/doc/bd1745587.html,er[c].listsize = LIST_INIT_SIZE;
while(1)
{
if(https://www.360docs.net/doc/bd1745587.html,er[c].length>=https://www.360docs.net/doc/bd1745587.html,er[c].listsize) //分配空间
{
newbase->file = (filetab *)realloc(https://www.360docs.net/doc/bd1745587.html,er[c].file,(https://www.360docs.net/doc/bd1745587.html,er
[c].listsize+LISTINCREMENT)*sizeof(filetab));
if(!newbase->file)
exit(-2);
https://www.360docs.net/doc/bd1745587.html,er[c].file = newbase->file;
https://www.360docs.net/doc/bd1745587.html,er[c].length+=LISTINCREMENT;
}
repeat:
fflush(stdin);
printf("请输入此用户的文件名\n");
fflush(stdin);
scanf("%c",&fname);
fflush(stdin);
for(i=0;i<=m;i++) //查找此文件是否被其他用户占有
{
if(fname==savename[i][1])
{
printf("此文件已经被别的用户占用请重新输入\n");
goto repeat;
}
}
for(i=0;i{
if(fname==a[i].name)
{
h=i; //h记录用户的文件
break;
}
else if(i==MAX1-1)
{
printf("不存在此文件重新输入\n");
goto repeat;
}
}
savename[h][1] = fname;
count1++;
//索引表中文件加一

https://www.360docs.net/doc/bd1745587.html,er[c].file[j].filename = fname;
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].ind = (indextab *)malloc(LIST_INIT_SIZE*sizeof(indextab));

//为文件索引表分配空间
if (!https://www.360docs.net/doc/bd1745587.html,er[c].file[j].ind)
exit(-2);
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].length=0;
https://www.360docs.net/doc/bd1745587.html,er[c].length++;

//用户文件长度加一
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].listsize = LIST_INIT_SIZE;
n = a[h].start;
for(k=0;;k++)
{
if(https://www.360docs.net/doc/bd1745587.html,er[c].file[j].length>=https://www.360docs.net/doc/bd1745587.html,er[c].file[j].listsize)
{
newbase->file[j].ind = (indextab *)realloc(https://www.360docs.net/doc/bd1745587.html,er[c].file
[j].ind,(https://www.360docs.net/doc/bd1745587.html,er[c].file[j].listsize+LISTINCREMENT)*sizeof(indextab));
if(!newbase->file[j].ind)
exit(-2);
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].ind = newbase->file[j].ind;
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].length+=LISTINCREMENT;
}
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].ind[k].order = k+1;
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].ind[k].block = n;
https://www.360docs.net/doc/bd1745587.html,er[c].file[j].length++;

if(n==-2)
break;
}
j++;
c++;
if(count1-1==filenum)
{
printf("fat表转换完毕!\n");
return;
}
while(1)
{
printf("此用户是否还有另外的文件?如果有文件输入1,否则输入0\n");
fflush(stdin);
scanf("%d",&flag);
fflush(stdin);
if(flag==1)
{
c--;
break;
}
else if(flag!=0)
{
printf("输入错误,重新输入\n");
continue;
}
else
break;
}
if(flag==0)
break;
}
}

void after()
{
int i,j,k;
printf("**********************************\n");
fflush(stdin);
for(k=0;k{
fflush(stdin);
printf("用户%s的索引

表为:\n",https://www.360docs.net/doc/bd1745587.html,er[k].name);
fflush(stdin);
for(i=0;i{
printf("文件名:%c\n",https://www.360docs.net/doc/bd1745587.html,er[k].file[i].filename);
printf("记录号:\t物理块号:\n");
for(j=0;jprintf("%4d\t%4d\n",https://www.360docs.net/doc/bd1745587.html,er[k].file[i].ind[j].order,https://www.360docs.net/doc/bd1745587.html,er
[k].file[i].ind[j].block);
}
}
}


void main()
{
char name[10];
int x,i;
while(1)
{
for(i=0;i<10;i++)
savename[i][1] ='\0';
printf("***************************************************************\n");
printf("\n\t模拟设计MS-DOS操作系统中磁盘文件的索引文件结构\n");
printf("\n\t\t选择1: 初始化fat");
printf("\n\t\t选择2: 显示fat表");
printf("\n\t\t选择3: 插入逻辑记录号操作");
printf("\n\t\t选择4: 保存文件操作");
printf("\n\t\t选择5: 转换索引表");
printf("\n\t\t选择6: 显示转换后的索引表");
printf("\n\t\t选择0: 结束");
printf ("\n\n***************************************************************\n");
printf("请输入选择的操作:");
scanf("%d",&x);

switch(x)
{
case 1:begin();
break;
case 2:display();
break;
case 3:insert();
break;
case 4:save();
break;
case 5:printf("\n*********************************************\n");
while(count1-1//在存入索引表的文件小于等于总文件个数的时候
{
printf("将fat表转换成索引表\n");
while(1)
{
printf("请输入用户名\n");
fflush(stdin);
scanf("%s",name);
change(name);
break;
}
}
break;
case 6:after();
break;
case 0:break;
}
if(x==0)
break;
}
}




相关文档
最新文档