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

📄 os.c

📁 模拟文件系统的功能
💻 C
📖 第 1 页 / 共 2 页
字号:
	printf("\ndelete file......");
	displayfile(currentuser);      //显示已经存在的文件目录
	printf("\n\t====input the file name:");
	scanf("%s",n);
	while(p!=NULL)
	{
		if(strcmp(p->filename,n)==0)   //顺序寻找文件名
		{
			if(p==currentuser->file)
			{
				printf("\nfile %s have been deleted success!",n);
				currentuser->file=currentuser->file->next;
				getchar();
				free(p);
			}
			else
			{
				pre->next=p->next;  //把前一个指针的NEXT指向N当前指针的NEXT,则删除该文件
				printf("\nfile %s have been deleted success!",n);
				getchar();
				free(p);
			}
			break;
		}
		else
		{
			pre=p;
			p=p->next;
		}
	}
	if(p==NULL)      //该文件不存在
	{
		printf("\n\nTIP:the file is not exist!");
	}
	displayfileoption();
}

void openfile()   //打开文件
{
	char n[10];
	UFD *temp=currentuser->file;//当前用户的文件
	AFD *t=currentopen,*tt=currentopen;//当前打开的文件
	int i=1;
	printf("\nopen file......");
	while(t!=NULL)
	{
		t=t->next;
		i++;
		if(i==6)
		{
			printf("\n\nopen too much file,you can open 5 files at most at the moment!");
			getchar();
			return;
		}
	}

	displayfile(currentuser);    //显示该用户的所有文件
	printf("\n\n\tthe files have been opened %s\n",currentuser->username);
	printf("\n\t====input the file name you want to open:");
	scanf("%s",n);
	while(temp!=NULL)
	{
		if(strcmp(temp->filename,n)==0)
		{
			t=get(AFD);//分配空间
			strcpy(t->filename,temp->filename);//要打开的文件名赋给当前打开的文件指针
			t->protect=temp->protect;
			t->length=temp->length;
			//t->text=NULL;
			t->next=NULL;
			if(currentopen==NULL)
			{
				currentopen=t;
			}
			else
			{
				while(tt->next!=NULL)
				{
					 tt=tt->next;
				}
				tt->next=t;//刚打开的文件放在链表的最后
			}

			printf("\nfile %s have been opened.",temp->filename);
			getchar();
			return;
		}
		else
		{
			temp=temp->next;//当前用户没文件
		}
		}
		if(temp==NULL)
		{
			printf("\n\ncan not file the file!");
			getchar();
		}
}

void readfile()     //读文件
{
	char n[10];
	AFD *t=currentopen;
	printf("\nread file......");
	if(currentopen==NULL)
	{
		printf("\n\nTIPS: you must open a file first!");
		getchar();
		return;
	}
	displayopen();     //显示已经打开的文件
	printf("\n\n\t====input the file name:");
	scanf("%s",n);
	while(t!=NULL)
	{
		if(strcmp(t->filename,n)==0)
		{
			printf("hello read");
			printf("\nfile\t\tcontent");
			printf("\n%s\t\t%s",t->filename,t->text);
			getchar();
			return;
        }
		else
		{
			t=t->next;
		}
	}
	printf("\n\n file can not find, open more files!!");
    getchar();
}
void displayproperty(MFD *pp)//显示同一用户下的文件属性
{
	UFD *temp=pp->file;
	if(temp==NULL)
	{
		printf("\nTIPS: \n      user %s is not exist\n    please create a new file!",pp->username);
		getchar();
		return;
	}
	printf("\n\n\t ..............>  %s  <.................",pp->username);
	printf("\n\t===============file property   ===========");
	printf("\n\tfile name\tfile length\tfile protect code\n");
	while(temp!=NULL)
	{
		printf("\n\t%s   \t%d\t\t%d\n",temp->filename,temp->length,temp->protect);
		temp=temp->next;
	}
	getchar();
}
	
void renamefile()
{
	MFD	*temp=currentuser;//当前用户的第一个文件
	char name[10],s;
	int flag=0;
	printf("\nrename file......");
	displayfile(currentuser);
	printf("\n\t====input the file you want to rename:");
	scanf("%s",name);
	while(temp->file!=NULL)
	{
		if(strcmp(temp->file->filename,name)==0)
		{
			printf("\nplease input a new file name:");
			scanf("%s",name);
			printf("\nDo you really want to chang file<%s>into new file<%s>? <y|n>",temp->file->filename,name);
			scanf("%s",&s);			
			switch(s)
			{
				case 'y':   strcpy(temp->file->filename,name);
							printf("\nmodify name success!\n");
							flag=1;
							break;
				case 'n':	break;
				default:	break;
			}
			if(flag)
				break;
		}
		else
			temp->file=temp->file->next;
	}//while end			
	getchar();
	displayfileoption();			
}

void writefile()      //读文件
{
	char n[10],temp[1024];
	AFD *t=currentopen;//当前打开的文件
	printf("\nwrite file......");
	if(currentopen==NULL)
	{
		printf("\n\nTIP: you must open a file first!");    //报错,还没打开文件
		getchar();
		return;
	}
	displayopen();//当前运行的文件
	printf("\n\t====input the file you want to write:");
	scanf("%s",n);
	while(t!=NULL)
	{
		if(strcmp(t->filename,n)==0)
		{
			if(t->protect==0)
			{
				printf("\n\\nthe file can not been writed!");    //该文件限制读写
				getchar();
				return;
			}
			break;
		}
		else
			t=t->next;
	}
	if(t==NULL)
	{
		printf("\n\ncan not file the file!");
		getchar();
		return;
	}
	//temp=(char *)malloc(sizeof(char)*t->length);
	printf("\n\nplease input the file content(length<=1024):");
	scanf("%s",temp);
	strcpy(t->text,temp); 
}

void closefile()     //关闭文件
{
	char n[10];
	AFD *p=currentopen,*pre=NULL;
	printf("\nclose file......");
	displayopen();
	printf("\n\ninput the file you want to close: ");
	scanf("%s",n);
	while(p!=NULL)
	{
		if(strcmp(p->filename,n)==0)
		{
			if(p==currentopen)	
			{
				printf("\n\nfile %s have been closed!",n);
				currentopen=currentopen->next;
				getchar();
				free(p);
			}
			else	
			{
				pre->next=p->next;
				printf("\n\nthe file have been closed!");
				getchar();
				free(p);
			}
			break;
		}
		else	
		{
			pre=p;
			p=p->next;
		}
	}
	if(p==NULL)
	{
		printf("\n\nTIP:you can not close the file!");
	}
	displayfileoption();
}

void copyfile(MFD *pp)//复制
{
	char name[10];
	UFD *temp=pp->file;
	printf("\ncopy file......");
	displayfile(currentuser);
	printf("\ninput the file you want to copy:");
	scanf("%s",name);
	while(temp!=NULL)
	{
		if(!strcmp(temp->filename,name))
		{
			buffer=get(UFD);
			strcpy(buffer->filename,temp->filename);
			buffer->length=temp->length;
			buffer->protect=temp->protect;
			buffer->next=NULL;
			break;
		}
		temp=temp->next;
	}
	displayfile(currentuser);
	getchar();
	displayfileoption();
}

void pastefile(MFD *pp)//粘贴
{
	UFD *temp=pp->file;
	printf("\npaste...... file\n");
	displayfile(currentuser);
	//printf("buffer content:%s,%d,%d\n",buffer->filename,buffer->length,buffer->protect);
	if(temp)
	{//此用户不空
		while(temp->next!=NULL)
			temp=temp->next;
		temp->next=buffer;
	}
	else
	{	
		pp->file=buffer;
	}
	getchar();
	displayfileoption();
}

void displayuser()     //显示用户名菜单
{
	int i=0;
	MFD *temp=user;
	printf("\n\t\t~~~~~~~ user list~~~~~~\n\n");
	while(temp!=NULL)
	{
		i++;
		printf("\t\t\tNO.%d:\t %s\n",i,temp->username);
		temp=temp->next;
	}
	if(user==NULL)
	{
		printf(" \n\t\t\t  no user exist!");
	}

}

void displayfile(MFD *pp)     //显示该用户的文件
{
	UFD *temp=pp->file;
	if(temp==NULL){
		printf("\nTIPS: \n      user %s have no filest\n    please create new file!",pp->username);
		getchar();
		return;
	}
	printf("\n\n\t ..............>  %s  <.................",pp->username);
	printf("\n\t===============file menu   ===========");
	printf("\n\tfile name\n");
	while(temp!=NULL){
		printf("\n\t%s\n",temp->filename);
		temp=temp->next;
	}
	getchar();
}

void displayopen()    //显示已经打开的文件
{
	AFD *temp=currentopen;
	int i=1;
	if(temp==NULL)
	{
		printf("\n\n\tplease open a file first!");
		getchar();
		return;
	}
	printf("\n\n\t__________________________");
	printf("\n\t       the file opened!\n");
	printf("\n\t-------------------------");
	while(temp!=NULL)	
	{
		i++;
		printf("\nfile opened%d:%s",i,temp->filename);
		temp=temp->next;

	}
}
void deleteafd()//删除当前运行的文件
{
	AFD *t=currentopen;
	while(t!=NULL)
	{
		currentopen=currentopen->next;//下一个文件
		free(t);//释放空间
		t=currentopen;
	}
}

int main()
{
	printf("\n\n\n\n\n");
	printf("\n\t***************************************************");
	printf("\n\n\t\t\tfile simulate system\n");
	printf("\n\t***************************************************");
	getchar();
	system("cls");
	displayuseroption();
	return 0;
}

⌨️ 快捷键说明

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