C语言文件系统设计
1.无需注册登录,支付后按照提示操作即可获取该资料.
2.资料以网页介绍的为准,下载后不会有水印.资料仅供学习参考之用.
密 惠 保
文件系统设计
1.文件系统
①下列几条命令
LOGIN 用户登陆
DIR 列文件目录
CREATE 创建文件
DELETE 删除文件
OPEN 打开文件
CLOSE 关闭文件
READ 读文件
WRITE 写文件
②列目录时要列出文件名,物理地址,保护码和文件长度。
③源文件可以进行读写保护。
①首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。
②用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2…并以编号作为物理地址,在目录中进行登记。
程序见下(本程序需要在c:下建一个名为osfile的目录及一个名为file的子目录):
#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"
#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/
#define MAXCHILD 50 /*the largest child*/
#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/ [资料来源:http://think58.com]
typedef struct /*the structure of OSFILE*/
{int fpaddr; /*file physical address*/
int flength; /*file length*/
int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/
char fname[MAXNAME]; /*file name*/
} OSFILE; copyright think58 [资料来源:THINK58.com]
typedef struct /*the structure of OSUFD*/
{char ufdname[MAXNAME]; /*ufd name*/
OSFILE ufdfile[MAXCHILD]; /*ufd own file*/
}OSUFD;
think58好,好think58 [资料来源:www.THINK58.com]
typedef struct /*the structure of OSUFD'LOGIN*/
{char ufdname[MAXNAME]; /*ufd name*/
char ufdpword[8]; /*ufd password*/
} OSUFD_LOGIN;
think58好,好think58 [来源:http://think58.com]
[资料来源:http://think58.com]
typedef struct /*file open mode*/
{int ifopen; /*ifopen:0-close,1-open*/
int openmode; /*0-read only,1-write only,2-read and write,3-initial*/
}OSUFD_OPENMODE;
think58好,好think58
[来源:http://www.think58.com]
OSUFD *ufd[MAXCHILD]; /*ufd and ufd own files*/
OSUFD_LOGIN ufd_lp;
think58 [资料来源:http://www.THINK58.com]
[来源:http://think58.com]int ucount=0; /*the count of mfd's ufds*/
int fcount[MAXCHILD]; /*the count of ufd's files*/
int loginsuc=0; /*whether login successfully*/
char username[MAXNAME]; /*record login user's name22*/
char dirname[MAXNAME];/*record current directory*/
int fpaddrno[MAX]; /*record file physical address num*/
OSUFD_OPENMODE ifopen[MAXCHILD][MAXCHILD]; /*record file open/close*/
int wgetchar; /*whether getchar()*/ [资料来源:http://think58.com]
FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file; copyright think58
void main()
{int i,j,choice1;
char choice[50]; /*choice operation:dir,create,delete,open,delete,modify,read,write*/
int choiceend=1; /*whether choice end*/
char *rtrim(char *str); /*remove the trailing blanks.*/
char *ltrim(char *str); /*remove the heading blanks.*/
void LoginF(); /*LOGIN FileSystem*/
void DirF(); /*Dir FileSystem*/
void CdF(); /*Change Dir*/
void CreateF(); /*Create File*/
void DeleteF(); /*Delete File*/
void ModifyFM(); /*Modify FileMode*/
void OpenF(); /*Open File*/
void CloseF(); /*Close File*/
void ReadF(); /*Read File*/
void WriteF(); /*Write File*/
void QuitF(); /*Quit FileSystem*/
void help();
if((fp_mfd=fopen("c:\\osfile\\mfd","rb"))==NULL)
{fp_mfd=fopen("c:\\osfile\\mfd","wb");
fclose(fp_mfd);
} think58好,好think58 [资料来源:http://www.THINK58.com]
for(i=0;i<MAX;i++) fpaddrno[i]=0;
textattr(BLACK*16|WHITE);
clrscr(); /*clear screen*/
LoginF(); /*user login*/
clrscr();
[资料来源:http://think58.com]
if(loginsuc==1) /*Login Successfully*/
{while (1)
{wgetchar=0;
if (choiceend==1)
{printf("\n\nC:\\%s>",strupr(dirname));}
else printf("Bad command or file name.\nC:\\%s>",strupr(username));
gets(choice);
strcpy(choice,ltrim(rtrim(strlwr(choice))));
if (strcmp(choice,"dir")==0) choice1=1;
else if(strcmp(choice,"creat")==0) choice1=2;
else if(strcmp(choice,"delete")==0) choice1=3;
else if(strcmp(choice,"attrib")==0) choice1=4;
else if(strcmp(choice,"open")==0) choice1=5;
else if(strcmp(choice,"close")==0) choice1=6;
else if(strcmp(choice,"read")==0) choice1=7;
else if(strcmp(choice,"modify")==0) choice1=8;
else if(strcmp(choice,"exit")==0) choice1=9;
else if(strcmp(choice,"cls")==0) choice1=10;
else if(strcmp(choice,"cd")==0) choice1=11; think58好,好think58
else if(strcmp(choice,"help")==0) choice1=20;
else choice1=12;
switch(choice1)
{case 1:DirF();choiceend=1;break;
case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;
case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;
case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();break;
case 5:choiceend=1;OpenF();if (!wgetchar) getchar();break;
case 6:choiceend=1;CloseF();if (!wgetchar) getchar();break;
case 7:choiceend=1;ReadF();if (!wgetchar) getchar();break;
case 8:choiceend=1;WriteF();if (!wgetchar) getchar();break;
case 9:printf("\nYou have exited this system.");
QuitF();exit(0);break;
case 10:choiceend=1;clrscr();break;
case 11:CdF();choiceend=1;break;
case 20:help();choiceend=1;break;
default:choiceend=0;
}
}
}
else printf("\nAccess denied.");
}
copyright think58 [资料来源:THINK58.com]
void help(void)
{
printf("\nThe Command List\n");
printf("\nCd Attrib Creat Modify Read Open Cls Delete Exit Close\n");
}
char *rtrim(char *str) /*remove the trailing blanks.*/
{int n=strlen(str)-1;
while(n>=0)
{if(*(str+n)!=' ')
{*(str+n+1)='\0';
break;
}
else n--;
}
if (n<0) str[0]='\0';
return str;
} copyright think58
[资料来源:www.THINK58.com]
char *ltrim(char *str) /*remove the heading blanks.*/
{char *rtrim(char *str);
strrev(str);
rtrim(str);
strrev(str);
return str;
} think58.com [资料来源:www.THINK58.com]
void LoginF() /*LOGIN FileSystem*/
{char loginame[MAXNAME],loginpw[9],logincpw[9],str[50];
int i,j,flag=1;
char a[25];
int findout; /*login user not exist*/
char *rtrim(char *str); /*remove the trailing blanks.*/
char *ltrim(char *str); /*remove the heading blanks.*/
void InputPW(char *password); /*input password,use '*' replace*/
void SetPANo(int RorW); /*Set physical address num*/
while(1)
{findout=0;
printf("\n\nLogin Name:");
gets(loginame);
ltrim(rtrim(loginame));
fp_mfd=fopen("c:\\osfile\\","rb");
for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i++)
if (strcmp(strupr(ufd_lp.ufdname),strupr(loginame))==0)
{findout=1;
strcpy(logincpw,ufd_lp.ufdpword);
}
fclose(fp_mfd);
if (findout==1) /*user exist*/
{printf("Login Password:");
InputPW(loginpw); /*input password,use '*' replace*/ think58.com
if (strcmp(loginpw,logincpw)==0)
{strcpy(username,strupr(loginame));
strcpy(dirname,username);
fp_mfd=fopen("c:\\osfile\\","rb");
for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)
{strcpy(str,"c:\\osfile\\");
strcat(str,ufd_lp.ufdname);
ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));
strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname));
fp_ufd=fopen(str,"rb");
fcount[j]=0;
for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)
{ifopen[j][i].ifopen=0;
ifopen[j][i].openmode=4;}
fclose(fp_ufd);}
fclose(fp_mfd);
ucount=j;
SetPANo(0);
printf("\n\nLogin successful! Welcome to this FileSystem\n\n");
loginsuc=1;
return;}
else
{printf("\n\n"); copyright think58
flag=1;
while(flag)
{printf("Login Failed! Password Error. Try Again(Y/N):");
gets(a);
ltrim(rtrim(a));
if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}
else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;}
}
}
}
else
{printf("New Password(<=8):");
InputPW(loginpw); /*input new password,use '*' replace*/
printf("\nConfirm Password(<=8):"); /*input new password,use '*' replace*/
InputPW(logincpw);
if (strcmp(loginpw,logincpw)==0)
{strcpy(ufd_lp.ufdname,strupr(loginame));
strcpy(ufd_lp.ufdpword,loginpw);
fp_mfd=fopen("c:\\osfile\\","ab");
fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd);
fclose(fp_mfd);
strcpy(username,strupr(loginame));
strcpy(dirname,loginame);
strcpy(str,"c:\\osfile\\");
strcat(str,username);
if((fp_ufd=fopen(str,"rb"))==NULL)
{fp_ufd=fopen(str,"wb");
fclose(fp_ufd);
}
fp_mfd=fopen("c:\\osfile\\","rb");
for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)
{strcpy(str,"c:\\osfile\\");
strcat(str,ufd_lp.ufdname);
ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));
strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname));
fp_ufd=fopen(str,"rb");
for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)
{ifopen[j][i].ifopen=0;
ifopen[j][i].openmode=4;}
fclose(fp_ufd);}
fclose(fp_mfd);
ucount=j;
SetPANo(0);
printf("\n\nLogin Successful! Welcome to this System\n\n");
loginsuc=1; think58 [来源:http://think58.com]
return;
}
else
{printf("\n\n");
flag=1;
while(flag)
{printf("Login Failed! Password Error. Try Again(Y/N):");
gets(a);
ltrim(rtrim(a));
if (strcmp(strupr(a),"Y")==0) {loginsuc=0;flag=0;}
else if(strcmp(strupr(a),"N")==0){loginsuc=0;flag=0;return;}
}
}
}
}
} think58.com [资料来源:www.THINK58.com]
void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/
{int i,j;
if (RorW==0)
{if((fp_file_p=fopen("c:\\osfile\\file\\file_p","rb"))==NULL)
{fp_file_p=fopen("c:\\osfile\\file\\file_p","wb");
fclose(fp_file_p);
}
fp_file_p=fopen("c:\\osfile\\file\\file_p","rb");
for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++)
fpaddrno[j]=1;
/*for(i=1;i<MAX;i++)
if ((i%13)==0) fpaddrno[i]=1;*/
}
else
{fp_file_p=fopen("c:\\osfile\\file\\file_p","wb");
/*for(i=1;i<MAX;i++)
if((i%13)==0) fpaddrno[i]=0;*/
for(i=0;i<MAX;i++)
if (fpaddrno[i]==1)
fwrite(&i,sizeof(int),1,fp_file_p);
}
fclose(fp_file_p);
} [来源:http://think58.com]
void InputPW(char *password) /*input password,use '*' replace*/
{int j;
内容来自think58 [资料来源:http://www.THINK58.com]