📄 1.txt
字号:
for(i=0;i<savenum;i++)
if(strcmp(tempfile,userfd[i].filename)==0)
break;
if(i>=savenum)
mesg("File not existed!");
else //文件存在的情况
{
for(i=0;i<opennum;i++)
if(strcmp(tempfile,openfd[i].filename)==0)
break;
if(i<opennum) //文件已经打开
{
mesg("File already opened!");
if(openfd[i].opencode!=tempcode)
{
openfd[i].opencode=tempcode;
mesg("File permission changed!");
}
}
else if(opennum>=L) //打开的文件已经占满了5个名额
mesg("Error! cannot open file! nimber of opened files limited!");
else //打开处理
{
strcpy(openfd[opennum].filename,tempfile);
openfd[opennum].opencode=tempcode;
workfile=opennum;
opennum++;
mesg("File open success!");
}
}
}
}
}
void myclose() //关闭文件
{
int i,j;
char tempfile[100];
if(strcmp(username,"")==0)
mesg("No user login!");
else
{
if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(tempfile,command->next->string);
for(i=0;i<savenum;i++)
if(strcmp(userfd[i].filename,tempfile)==0)
break;
if(i>=savenum) //文件不存在
mesg("the file donot existed!");
else
{
for(j=0;j<opennum;j++)
if(strcmp(openfd[j].filename,tempfile)==0)
break;
if(j>=opennum) //文件存在了但是没有打开
mesg("the file already close!");
else //文件存在并且打开
{
strcpy(openfd[j].filename,"");
openfd[j].opencode=' ';
opennum--;
mesg("File close success!");
}
}
}
}
}
void mydelete() //删除文件
{
int i,j;
int tempsave; //用于确定被删除文件在数组userfd[M]中的位置
char tempfile[100];
FILE *fp;
if(strcmp(username,"")==0)
mesg("No user login!");
else
{
if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(tempfile,command->next->string);
for(i=0;i<savenum;i++)
if(strcmp(userfd[i].filename,tempfile)==0)
break;
tempsave=i;
if(tempsave>=savenum) //文件不存在
mesg("the file donot existed!");
else
{
for(j=0;j<opennum;j++)
if(strcmp(openfd[j].filename,tempfile)==0)
break;
if(j<opennum) //文件存在了但是打开
{
mesg("File can't delete!");
mesg("Close it first!");
}
else //文件存在并且没有打开
{
strcpy(userfd[tempsave].filename,"");
userfd[tempsave].protect=' ';
userfd[tempsave].length=0;
savenum--;
for(i=tempsave;i<=savenum;i++) //从新将数组userfd[M]排序
{
strcpy(userfd[i].filename,userfd[i+1].filename);
userfd[i].protect=userfd[i+1].protect;
userfd[i].length=userfd[i+1].protect;
}
mesg("File delete!");
fp=fopen(username,"w+"); //写回到文件username
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}
fclose(fp);
}
}
}
}
}
void myread() //读操作
{
int i;
int tempsave;
char tempfile[100];
if(strcmp(username,"")==0)
mesg("No user login!");
else if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(tempfile,command->next->string);
for(i=0;i<savenum;i++)
if(strcmp(tempfile,userfd[i].filename)==0)
break;
if(i>=savenum)
mesg("the File not existed!");
else
{
tempsave=i;
for(i=0;i<opennum;i++)
if(strcmp(tempfile,openfd[i].filename)==0)
break;
if(i>=opennum)
mesg("File not opened!");
else
{
if(userfd[tempsave].length<1000)
printf("\n The file size is %d B",userfd[tempsave].length);
else
if(userfd[tempsave].length=1000)
printf("\n The file size is 1000 B");
else
printf("\n The file size is %d,%d KB",
userfd[tempsave].length/1000,userfd[tempsave].length%1000);
mesg("File read!");
}
}
}
}
void mywrite() //写操作
{
int i;
int tempsave;
char tempfile[100];
if(strcmp(username,"")==0)
mesg("No user login!");
else if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(tempfile,command->next->string);
for(i=0;i<savenum;i++)
if(strcmp(tempfile,userfd[i].filename)==0)
break;
if(i>=savenum)
mesg("the File not existed!");
else
{
tempsave=i;
for(i=0;i<opennum;i++)
if(strcmp(tempfile,openfd[i].filename)==0)
break;
if(i>=opennum)
mesg("File not opened!");
else
{
if(userfd[tempsave].length<1000)
printf("\n The file size is %d KB",userfd[tempsave].length);
else
if(userfd[tempsave].length=1000)
printf("\n The file size is 1000KB");
else
printf("\n The file size is %d,%d KB",
userfd[tempsave].length/1000,userfd[tempsave].length%1000);
mesg("File written!");
}
}
}
}
void help() //帮助说明
{
static char *cmd[]={"login","setpass","logout","create",
"open","read","write","delete","dir",
"help","exit","copy","rename"};
static char *cmdhlp[]={"command format:login [<username>]",
"command format:setpass",
"command format:logout",
"command format:create <filename>",
"command format:open [/r|/w|/d] <filename>",
"command format:read <filename>",
"command format:write <filename>",
"command format:delete <filename>",
"command format:dir [/u|/o|/f]",
"command format:help [<command>]",
"command format:exit",
"command format:copy <source filename> <destination filename>",
"command format:rename <old filename> <new filename>"};
static char *detail[]={"explain:user login in the file system.",
"explain:modify the user password.",
"explain:user logout the file system.",
"explain:create a new file.",
"explain:/r--read only [deflaut] \n\t /w--read and modify \n\t /d--read modify and delete.",
"explain:read the file.",
"explain:modify a new file.",
"explain:delete a new file.",
"explain:/u--list the user account\n\t /o--list opened files \n\t /f --list user files [deflaut]",
"explain:<command>--list the command detail format andexplain.\n\t [deflaut] list the command.",
"explain:exit from the file system",
"explain:copy from one file to another file",
"explain:modify the file name."};
int helpnum=13;
int i;
if(command->next==NULL)
{
mesg(cmdhlp[9]);
mesg(detail[9]);
mesg("step 1:login");
printf("\t if old user then login,if user not exist then create new user");
mesg("step 2:open the file for read(/r),write(/w) or delete(/d)");
printf(" /t you can open one or more files one command can open one file");
mesg("step 3:read,write or delete some one file");
printf(" /t you can oprate one of the opened files. one command can open one file");
mesg("step 4:close the open file");
printf(" /t you can close one of the opened files. one command can open one file");
mesg("step 5:user logout.close all the user opened files.");
printf(" \n command list:");
for(i=0;i<helpnum;i++)
{
if(i%4==0)
printf("\n");
printf("%-10s",cmd[i]);
}
}
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
for(i=0;i<helpnum;i++)
if(strcmp(command->next->string,cmd[i])==0)
break;
if(i>=helpnum)
mesg("the command not existed!");
else
{
mesg(cmdhlp[i]);
mesg(detail[i]);
}
}
}
void dir() //显示操作
{
int i,j;
int flag=0;
if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else if(strcmp(command->next->string,"/f")==0)
{
if(strcmp(username,"")==0)
{
mesg("No user login!");
mesg("login first!");
return;
}
printf("total user files: %d file(s) ",savenum);
printf("20 files limited!\n");
for(i=0;i<savenum;i++)
{
if(strlen(userfd[i].filename)>0)
{
printf("%s ",userfd[i].filename);
printf("%dKB\n",userfd[i].length);
}
}
}
else if(strcmp(command->next->string,"/o")==0)
{
if(strcmp(username,"")==0)
{
mesg("No user login!");
mesg("login first!");
return;
}
printf("total user opened files: %d file(s) ",opennum);
printf("5 opened files limited!\n");
for(i=0;i<opennum;i++)
{
printf("%s ",openfd[i].filename);
flag=openfd[i].opencode;
if(flag='r')
printf("read only\n");
else if(flag='w')
printf("read and modify\n");
else if(flag='d')
printf("read modify and delete\n");
else
printf("noknow operater\n");
}
}
else if(strcmp(command->next->string,"/u")==0)
{
j=0;
for(i=0;i<M;i++)
if(strlen(mainfd[i].username)>0)
j++;
printf("total user : %d file(s) ",j);
printf("30 files limited!\n");
for(i=0;i<usernum;i++)
printf("%s\n",mainfd[i].username);
}
else if(command->next->string[0]=='/')
mesg("/u-list the user account /o-list opened files /f-list user files");
else
mesg("Error operating!");
}
void mycopy()
{
int i,j,temp;
char sourfile[256],destfile[256];
FILE *fp;
char tempchar;
if(strcmp(username,"")==0)
mesg("no user login!");
else if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next==NULL)
mesg("Too few parameters!");
else if(command->next->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(sourfile,command->next->string);
strcpy(destfile,command->next->next->string);
for(i=0;i<savenum;i++)
if(strcmp(sourfile,userfd[i].filename)==0)
break;
temp=i;
if(i>=savenum)
printf("\n the source file not exist!");
else
{
for(i=0;i<opennum;i++)
if(strcmp(sourfile,openfd[i].filename)==0)
break;
if(i>=opennum) printf("\n the source file not opened!");
else
{
for(j=0;j<opennum;j++)
if(strcmp(destfile,openfd[j].filename)==0)
break;
if(j<opennum) mesg("Error! the destination file opened,you must close it first.");
else
{
for(j=0;j<savenum;j++)
if(strcmp(destfile,userfd[j].filename)==0)
break;
if(j<opennum)
{
mesg("Warning! the destination file exist!");
printf("\n Are you sure to recover it?[Y/N]");
while(!kbhit());
tempchar=getch();
if(tempchar=='N'||tempchar=='n')
mesg("Cancel! file not copy.");
else if(tempchar=='Y'||tempchar=='y')
{
mesg("File copy success! file recovered!");
userfd[j].length=userfd[i].length;
userfd[j].protect=userfd[i].protect;
fp=fopen(username,"w+");
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}
fclose(fp);
}
}
else if(savenum>=M)
mesg("copy false! file total limited.");
else
{
strcpy(userfd[savenum].filename,destfile);
userfd[savenum].length=userfd[temp].length;
userfd[savenum].protect=userfd[temp].protect;
savenum++;
fp=fopen(username,"w+");
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}
fclose(fp);
mesg("File copy success!");
}
}
}
}
}
}
void myrename()
{
int i,j,temp;
char oldfile[256],newfile[256];
FILE *fp;
if(strcmp(username,"")==0)
mesg("No user login!");
else if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next==NULL)
mesg("Too few parameters!");
else if(command->next->next->next!=NULL)
mesg("Too many parameters!");
else
{
strcpy(oldfile,command->next->string);
strcpy(newfile,command->next->next->string);
for(i=0;i<savenum;i++)
if(strcmp(userfd[i].filename,oldfile)==0)
break;
temp=i;
if(temp>=savenum) //文件不存在
mesg("the soruce file donot existed!");
else
{
for(j=0;j<opennum;j++)
if(strcmp(openfd[j].filename,oldfile)==0)
break;
if(j<opennum) //文件存在了但是打开
mesg("Source file mustbe close!");
else //文件存在并且没有打开
{
strcpy(userfd[temp].filename,newfile);
mesg("File rename success!");
fp=fopen(username,"w+"); //写回到文件username
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}
fclose(fp);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -