⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fms.cpp

📁 在控制台下模拟的windows操作系统下的文件管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		mesg("Too few parametets!");
	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("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].lenght<1000)
					printf("\n The file size is %d KB",userfd[tempsave].lenght);
				else if(userfd[tempsave].lenght==1000)
					printf("\n The file size is 1000 KB");
				else
					printf("\n The file size is %d,%d KB",userfd[tempsave].lenght/1000,userfd[tempsave].lenght%1000);
				mesg("File read");
			}
		}
	}
}
void myopen()/*打开*/
{
	int i=0;
	int type=0;
	char tempcode;
	char tempfile[100];
	if(strcmp(username,"")==0)
		mesg("No user login!");
	else if(command->next==NULL)
		mesg("Too few parameters!");
	else
	{
		strcpy(tempfile,"");
		tempcode='r';
		if(strcmp(command->next->string,"/r")==0)
		{
			tempcode='r';
			type=1;
		}
		else if(strcmp(command->next->string,"/w")==0)
		{
			tempcode='w';
			type=1;
		}
		else if(strcmp(command->next->string,"/d")==0)
		{
			tempcode='d';
			type=1;
		}
		else if(command->next->string[0]=='/')
			mesg("Error! /r/w/d request!");
		else if(command->next->next!=NULL)
			mesg("Too many parameters!");
		else
			strcpy(tempfile,command->next->string);
		/////////////////////////////////////////////////////

		//////////////////////////////////////////////////////
		if(type==1)
		{
			if(command->next->next!=NULL)
			{
				if(command->next->next->next!=NULL)
					mesg("Too many parameters!");
				else
					strcpy(tempfile,command->next->next->string);
			}
			else
				mesg("Too few parameters!");
		}
		if(strcmp(tempfile,""))
			{
				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)
					{
						mesg("Error! connot open file! number of opened files limited!");
					}
					else
					{
						strcpy(openfd[opennum].filename,tempfile);
						openfd[opennum].opencode=tempcode;
						workfile=opennum;
						opennum++;
						mesg("File open sucess!");
					}
				}
			}
	}

}
void myclose()/*关闭*/
{
		int i=0;
	int tempsave=0;
	char tempfile[100];
		if(strcmp(username,"")==0)
		mesg("No user lgoin!");
	else if(command->next==NULL)
		mesg("Too few parametets!");
	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("File not existed!");
		else
		{
			for(i=0;i<opennum;i++)
			{
				if(strcmp(tempfile,openfd[i].filename)==0)
					break;
			}
			if(i>=opennum)
				mesg("File not opened");
			else
			{
				if(i==opennum-1)
					opennum--;
				else
				{
					for(;i<opennum-1;i++)
					{
						strcpy(openfd[i].filename,openfd[i+1].filename);
						openfd[i].opencode=openfd[i+1].opencode;
					}
					opennum--;
				}
				mesg("Close file success!");
			}
		}
	}
}
void mywrite()/*写*/
{
	int i=0;
	int tempsave=0;
	char tempfile[100];
	if(strcmp(username,"")==0)
		mesg("No user lgoin!");
	else if(command->next==NULL)
		mesg("Too few parametets!");
	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("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
			{
				mesg("File write");
				int tt=rand();
				if(userfd[tempsave].lenght<=1000)
					printf("\n The file size  from %d KB to %d KB",userfd[tempsave].lenght,userfd[tempsave].lenght+tt);
			/*	else if(userfd[tempsave].lenght=1000)
					printf("\n The file size from 1000 KB");*/
				else
					printf("\n The file size form %d,%d KB to %d,%d KB",userfd[tempsave].lenght/1000,userfd[tempsave].lenght%1000,userfd[tempsave].lenght+tt/1000,userfd[tempsave].lenght+tt%1000);
				userfd[tempsave].lenght=userfd[tempsave].lenght+tt;
			}
		}
	}
}
void help()/*帮助*/
{
	static char *cmd[]={"login","setpass","logout","create",
		               "open","read","write","delete","dir",
					   "help","exit","copy","rename"};
	static char *cmdhlp[]={"aommand 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:creat a new file.",
						   "explain:/r -- read only [deflaut]\n\t   /w -- list opened files \n\t  /d --read,modify and delete.",
						   "explain:read the file.",
						   "explain:modify the file.",
						   "explain:delete the file.",
						   "explain:/u -- list the user account\n\t  /0 -- list opened files\n\t  /f --list user file[deflaut].",
						   "explain:<command> -- list the command detail format and explain.\n\t  [deflaut] list the command.",
						   "explain:exit from the file sysytem.",
						   "explain:copy from one file to another file.",
						   "explain:modify the file name."};
	int helpnum=13;
	int i=0;
	if(command->next==NULL)
	{
		mesg(cmdhlp[9]);
		mesg(detail[9]);
		mesg("step 1: login");
		printf("\t if 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 operate 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 oepned files.one command can open one file");
		mesg("step 5: user loout.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=0;
	int type=0;
	char tempcode;
	if(strcmp(username,"")==0)
		mesg("No user login!");
	else
	{
		if(command->next==NULL)
		{
			tempcode='f';
		}
		else
		{
			if(strcmp(command->next->string,"/u")==0)
			{
				tempcode='u';
			}
			else if(strcmp(command->next->string,"/o")==0)
			{
				tempcode='o';
			}
			else if(strcmp(command->next->string,"/f")==0)
			{
				tempcode='f';
			}
			else if(command->next->string[0]=='/')
				mesg("Error! /u/o/f request!");
			else if(command->next->next!=NULL)
				mesg("Too many parameters!");
		}
		if('u'==tempcode)
		{
			printf("list the user account\n");
			printf("usename\n");
			for(i=0;i<usernum;i++)
			{
				printf("%s\n",mainfd[i].username);
			}
		}
		else if('f'==tempcode)
		{
			printf("list opened files\n");
			printf("filename       length     protect\n");
			for(i=0;i<savenum;i++)
			{
				printf("%s%s%d%s%s%c\n",userfd[i].filename,"      ",userfd[i].lenght,"KB","      ",userfd[i].protect);
			}
		}
		else if('o'==tempcode)
		{
			printf("list user files\n");
			printf("filename       opencode\n");
			for(i=0;i<opennum;i++)
			{
				printf("%s%s%c\n",openfd[i].filename,"      ",openfd[i].opencode);
			}
		}
	}
}
void mycopy()/*复制*/
{
	char sourfile[256],destfile[256];
	int i=0,j=0,temp=0;
	FILE *fp;
	char tempchar;
	if(strcmp(username,"")==0)
		mesg("No user lgoin!");
	else if(command->next==NULL)
		mesg("Too few parametets!");
	else if(command->next->next==NULL)
		mesg("Too few parametets!");
	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 eixsted!");
		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[i].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[i].filename)==0)
							break;
					}
					if(j<savenum)
					{
						mesg("Warning! the destination file exist!");
						printf("\n  Are you sure to recover if ? [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].lenght=userfd[i].lenght;
							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].lenght);
							}
							fclose(fp);
						}
					}
					else if(savenum>=M)
						mesg("copy false!  file total limited.");
					else
					{
						strcpy(userfd[savenum].filename,destfile);
						userfd[savenum].lenght=userfd[temp].lenght;
						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].lenght);
						}
						fclose(fp);
						mesg("File copy success!");
					}
				}
			}
		}
	}
}
void myrename()/*文件改名*/
{
	char oldfile[256],newfile[256];
	int i=0,temp=0;
	if(strcmp(username,"")==0)
		mesg("No user lgoin!");
	else if(command->next==NULL)
		mesg("Too few parametets!");
	else if(command->next->next==NULL)
		mesg("Too few parametets!");
	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(oldfile,userfd[i].filename)==0)
				break;
		}
		temp=i;
		
		if(temp>=savenum)
			printf("\n  the source file not eixsted!");
		else if(temp!=i&&i<savenum)
		{
			printf("\n  newfile and the existing files is the same name, please re-naming!");
		}
		else
		{
			for(i=0;i<opennum;i++)
			{
				if(strcmp(oldfile,openfd[i].filename)==0)
					break;
			}
			if(i>=opennum)
				printf("\n the source file not opened!");
			else if(strcmp(oldfile,newfile)==0)
			{
				printf("Rename false! oldfile and newfile can not be the same.");
			}
			else
			{
				strcpy(openfd[i].filename,newfile);
				strcpy(userfd[temp].filename,newfile);
				mesg("Rename file success!\n");
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -