操作系统文件系统管理Word格式文档下载.docx
《操作系统文件系统管理Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《操作系统文件系统管理Word格式文档下载.docx(35页珍藏版)》请在冰豆网上搜索。
#defineFILENAME_LENGTH10//最大用户数
#definecommandAmount9//指令数
char_user[20];
//当前用户名
charcurrentDir[255];
//当前所在目录
typedefstructUserMess//所有用户信息
{
char_UserName[20];
char_UserPwd[20];
structUserMess*nextUser;
}_UserMess;
_UserMess*firstUser;
typedefstructFolderDir//目录路径信息
{
char_Dir[255];
structFolderDir*fatherDir;
}_RootDir;
_RootDir*rootFolder;
typedefstructFolder//当前目录子目录信息
char_FolderName[100];
structFolder*nextFolder;
}_Folder;
_Folder*firstFolder;
typedefstructFile//当前目录下文件信息
char_FileName[100];
structFile*nextFile;
}_File;
_File*firstFile;
【程序说明】
1.功能函数设计:
voidinit();
//初始化部分内容
intReadUserMess();
//读取已有用户信息
intuserCreate(intsumUsers);
//用户注册
intlogin();
//登录
voidcreateRootFolder(charusername[]);
//为新用户分配空间
voidTraverseFolder(charpath[]);
//遍历文件及文件夹
voidListFileAndFolder();
//显示当前文件夹下所有文件
voidChangeDir(charfolderName[]);
//目录跟换
voidDelete(charfName[]);
//删除文件或文件夹
voidCatFile(charfName[]);
//打开文件,查看文件内容
voidAddContent(charfName[]);
//向文件写入内容
voidCreateFile(charfName[]);
//创建文件
voidCreateFolder(charfolderName[]);
//创建文件夹
voidHelpFile();
//帮助文件
2.程序代码:
#include<
stdio.h>
stdlib.h>
string.h>
sys/types.h>
dirent.h>
#defineMaxUser20
#definecommandAmount9
typedefstructUserMess
typedefstructFolderDir
typedefstructFolder
typedefstructFile
intmain()
intlogined=0;
intselect;
charch;
charorder[commandAmount][9];
strcpy(order[0],"
create"
);
strcpy(order[1],"
rm"
strcpy(order[2],"
cat"
strcpy(order[3],"
write"
strcpy(order[4],"
ls"
strcpy(order[5],"
cd"
strcpy(order[6],"
exit"
strcpy(order[7],"
mkdir"
strcpy(order[8],"
help"
charcommand[15],command_str1[255];
inti,j;
init();
intuserNum=ReadUserMess();
//printf("
Usercount:
%d\n"
userNum);
intisright=1;
while(isright==1)
{
printf("
********************************************\n"
1、Creatuser\n"
2、login\n"
Pleasechoocethefunctionkey:
>
"
intchoice;
setbuf(stdin,NULL);
scanf("
%d"
&
choice);
if(choice==1)
logined=userCreate(userNum);
isright=0;
}
elseif(choice==2)
logined=login(userNum);
TraverseFolder(currentDir);
if(logined==1)
while(logined)
//command[0]=0;
for(i=0;
i<
15;
i++)
command[i]=0;
255;
command_str1[i]=0;
//command_str1[0]=0;
\n%s:
~$"
_user);
for(i=0,j=0;
ch=getchar();
if(j==1)
if(ch=='
'
||ch=='
\n'
)
break;
else
command_str1[i]=ch;
}
if(j==0)
i=-1;
j++;
elseif(ch=='
command[i]=ch;
\n"
commandAmount;
if(!
strcmp(command,order[i]))
select=i;
if(i==commandAmount)
Undefinedcommand!
continue;
switch(select)
case0:
if(command_str1!
=NULL)
CreateFile(command_str1);
Donothasnormalfilename,createfailed!
case1:
Delete(command_str1);
Donothasnormalfileordirname,createfailed!
case2:
CatFile(command_str1);
case3:
AddContent(command_str1);
case4:
ListFileAndFolder();
case5:
ChangeDir(command_str1);
Donothasnormaldirname,createfailed!
case6:
logined=0;
case7:
CreateFolder(command_str1);
case8:
HelpFile();
}
voidHelpFile()
printf("
—————————————————————————————————————\n"
create-创建格式:
createa1,将创建名为a1的文件\n"
rm-删除格式:
rma1,将删除名为a1的文件或文件夹\n"
cat-查看文件内容格式:
cata1,显示a1的内容\n"
write-写入格式:
writea1\n"
ls-显示文件格式:
ls,显示所有文件\n"
cd-跳转目录格式:
cddir或cd..dir为路径\n"
exit-退出程序格式:
exit\n"
mkdir-创建文件夹格式:
mkdira1,将创建文件夹a1\n"
help-查看帮助格式:
help\n"
————————————————————————————————————————\n"
voidCreateFile(charfName[])
charfSavePath[255];
strcpy(fSavePath,currentDir);
strcat(fSavePath,"
/"
strcat(fSavePath,fName);
_File*fi;
fi=firstFile;
while(fi->
nextFile!
=NULL)
if(strcmp(fi->
_FileName,fName)==0)
FilesisExist!
Createfailed!
return;
fi=fi->
nextFile;
FILE*fp;
fp=fopen(fSavePath,"
w"
if(fp==NULL)
Filecreatefailed!
else
Filecreatesuccess!
voidTraverseFolder(charpath[])
_Folder*p;
_File*q;
firstFolder=(_Folder*)malloc(sizeof(_Folder));
firstFolder->
nextFolder=NULL;
firstFile=(_File*)malloc(sizeof(_File));
firstFile->
nextFile=NULL;
structdirent*entry=NULL;
DIR*pDir;
pDir=opendir(path);
while(NULL!
=(entry=readdir(pDir)))
if(entry->
d_type==8)
q=(_File*)malloc(sizeof(_File));
strcpy(q->
_FileName,entry->
d_name);
q->
nextFile=firstFile;
firstFile=q;
//printf("
%s"
entry->
elseif(entry->
d_type==4)
if((strcmp(entry->
d_name,"
."
)!
=0)&
&
(strcmp(entry->
.."
(entry->
d_name[0]!
='
.'
))
p=(_Folder*)malloc(sizeof(_Folder));
strcpy(p->
_FolderName,entry->
p->
nextFolder=firstFolder;
firstFolder=p;
Folder:
//d_type:
4表示目录,8表示文件
voidListFileAndFolder()
\n************************************************************\n"
Files:
fi->
_FileName);
fi=fi->
\n\n"
_Folder*fo;
fo=firstFolder;
Folders:
while(fo->
nextFolder!
fo->
_FolderName);
fo=fo->
nextFolder;
****************************************************************\n"
voidDelete(charfName[])
%s"
fName);
intisExist=0;
//文件或文件夹存储完整路径
isExist=1;
if(isExist==1)
remove(fSavePath);
\nFile\"
%s\"
deletesuccess!
fo=firstFolder;
nextFolder!
if(strcmp(fo->
_FolderName,fName)==0)
fo=fo->
rmdir(fSavePath);
\nFolder\"
voidCatFile(charfName[])
r"
Filenotexist!
\n%s\'
sContent:
****************************\n"
ch=fgetc(fp);
while(ch!
=EOF)
if(ch=='
%c"
ch);
\n***End*************************************\n"
voidCreateFolder(charfolderName[])
intdir;
charfolderPath[255];
_FolderName,folderName)==0)
FolderisExist!
fo=fo->
strcpy(folderPath,currentDir);
strcat(folderPath,"
strcat(folderPath,folderName);
dir=mkdir(folderPath,0777);
if(dir==-1)