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

📄 文件系统.cpp

📁 操作系统的文件系统程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	int file_mode;
	char file_name[MAXNAME];                        //用于保存文件名
	char str[MAXNAME];
	cout<<"input file name:";
	cin>>file_name;                                 //读入文件名
	int i = existFile(file_name);
	while(i==-1)
	{
		cout<<"The file is not existed."<<endl<<endl;
		cout<<"input file name:";
		cin>>file_name;                                 //读入文件名
		i = existFile(file_name);
	}

	int j = existDirname(dirname);
	if(openflag[j][i].openflag==1)
		cout<<"The file has been opened."<<endl;
	else
	{
		if(userfile[j]->userAllFile[i].fmode==3)
			cout<<"Open failed.\nThe file's mode is protect."<<endl;
		else
		{
			cout<<"0-read only, 1-write only, 2-read and write)"<<endl;
			cout<<"Input the open mode:";
			cin>>file_mode;
			while(file_mode<0 || file_mode>2)
			{
				cout<<"Open failed.\nThe open mode is not exist."<<endl<<endl;
				cout<<"0-read onlu, 1-write only, 2-read and write)"<<endl;
				cout<<"Input the open mode:";
				cin>>file_mode;
			}
			if(file_mode==0)
			{
				strcpy(str,"read only");						//复制模式到字符串
				if(userfile[j]->userAllFile[i].fmode==0||userfile[j]->userAllFile[i].fmode==2)
					openflag[j][i].openflag = 1;
				else cout<<"The file cannot be opened."<<endl;
			}
			else if(file_mode==1)
			{
				strcpy(str,"write only");						//复制模式到字符串
				if(userfile[j]->userAllFile[i].fmode==1||userfile[j]->userAllFile[i].fmode==2)
					openflag[j][i].openflag = 1;
				else cout<<"The file cannot be opened."<<endl;
			}
			else if(file_mode==2)
			{
				strcpy(str,"read and write");						//复制模式到字符串
				if(userfile[j]->userAllFile[i].fmode==2)
					openflag[j][i].openflag = 1;
				else cout<<"The file cannot be opened."<<endl;
			}
			if(openflag[j][i].openflag==1)
			{
				openflag[j][i].openmode  = file_mode;
				cout<<file_name<<" has been opened by "<<str<<" mode."<<endl;
			}
		}
	}
	cout<<endl;
}

void closeFile()
{
	int n=0;
	char file_name[MAXNAME];                        //用于保存文件名

	int j=existDirname(dirname);					//判断路径并将其ID存放在变量中
	cout<<"The following file(s) has been opened:"<<endl;	//输出提示
	for(int i=0;i<fileCount[j];i++)					//循环所有文件
	{
		if(openflag[j][i].openflag==1)				//若文件已打开
		{
			cout<<"\t"<<userfile[j]->userAllFile[i].fname;	//输出文件名
			n++;								//记录打开文件数变量加1
		}
		if(n%4==0 && n!=0)						//若一行已输出4个变量
			cout<<endl;							//输出回车
	}
	cout<<endl<<n<<" file(s) opened."<<endl;	//输出总共被打开的文件数
	if(n==0)									//若无文件被打开
	{
		cout<<"No file can be closed."<<endl;	//输出提示
		return;									//返回
	}

	cout<<"input file name:";
	cin>>file_name;                                 //读入文件名
	i = existFile(file_name);
	while(i==-1)
	{
		cout<<"The file has not existed."<<endl<<endl;
		cout<<"input file name:";
		cin>>file_name;                                 //读入文件名
		i = existFile(file_name);
	}

	if(openflag[j][i].openflag==0)
		cout<<"Closed failed.\nThe file has been closed."<<endl;
	else
	{
		openflag[j][i].openflag = 0;
		openflag[j][i].openmode = 4;
		cout<<"The file  has been closed."<<endl;
	}
	cout<<endl;
}

void readFile()
{
	int n=0;
	char file_name[MAXNAME];                        //用于保存文件名
	char str[MAXNAME],str1[MAXNAME],c;

	int j=existDirname(dirname);					//判断路径并将其ID存放在变量中
	cout<<"The following file(s) has been opened:"<<endl;	//输出提示
	for(int i=0;i<fileCount[j];i++)					//循环所有文件
	{
		if(openflag[j][i].openflag==1)				//若文件已打开
		{
			cout<<"\t"<<userfile[j]->userAllFile[i].fname;	//输出文件名
			n++;								//记录打开文件数变量加1
		}
		if(n%4==0 && n!=0)						//若一行已输出4个变量
			cout<<endl;							//输出回车
	}
	cout<<endl<<n<<" file(s) opened."<<endl;	//输出总共被打开的文件数
	if(n==0)									//若无文件被打开
	{
		cout<<"No file can be read."<<endl;	//输出提示
		return;									//返回
	}

	cout<<"input file name:";
	cin>>file_name;                                 //读入文件名
	i = existFile(file_name);
	while(i==-1)
	{
		cout<<"The file has not existed."<<endl<<endl;
		cout<<"input file name:";
		cin>>file_name;                                 //读入文件名
		i = existFile(file_name);
	}

	if(openflag[j][i].openflag==0)				//若文件未被打开
		cout<<"Read failed.\nThe file has not been opened."<<endl;
	else
	{
		if(openflag[j][i].openmode==0||openflag[j][i].openmode==2)
		{
			itoa(userfile[j]->userAllFile[i].fileAddress,str,10);
			strcpy(str1,"ownfile\\myfile\\file");		//复制地址到路径
			strcat(str1,str);						//追加文件ID
			fp_file=fopen(str1,"rb");				//打开文件
			fseek(fp_file,0,0);						//文件指针指到头
			cout<<"The text is:"<<endl;				//输出提示
			while(fscanf(fp_file,"%c",&c)!=EOF)		//当文件未结束
			{
				if(c=='\n')	cout<<endl;				//若读到换行,输出回车
				else		cout<<c;				//若读到字符,输出
			}
			cout<<userfile[j]->userAllFile[i].fileLength<<" length"<<endl;	//输出文件长度
			fclose(fp_file);						//关闭文件
		}
		else										//若文件不可读
			cout<<"The file cannot be read."<<endl;	//输出提示
	}
	cout<<endl;
}

/*void modifyFile()
{
}*/

void writeFile()
{
	
	int n=0;
	char file_name[MAXNAME];                        //用于保存文件名
	char str[MAXNAME],str1[MAXNAME];
	int c;

	int j=existDirname(dirname);					//判断路径并将其ID存放在变量中
	cout<<"The following file(s) has been opened:"<<endl;	//输出提示
	for(int i=0;i<fileCount[j];i++)					//循环所有文件
	{
		if(openflag[j][i].openflag==1)				//若文件已打开
		{
			cout<<"\t"<<userfile[j]->userAllFile[i].fname;	//输出文件名
			n++;								//记录打开文件数变量加1
		}
		if(n%4==0 && n!=0)						//若一行已输出4个变量
			cout<<endl;							//输出回车
	}
	cout<<endl<<n<<" file(s) opened."<<endl;	//输出总共被打开的文件数
	if(n==0)									//若无文件被打开
	{
		cout<<"No file can be read."<<endl;	//输出提示
		return;									//返回
	}

	cout<<"input file name:";
	cin>>file_name;                                 //读入文件名
	i = existFile(file_name);
	while(i==-1)
	{
		cout<<"The file has not existed."<<endl<<endl;
		cout<<"input file name:";
		cin>>file_name;                                 //读入文件名
		i = existFile(file_name);
	}

	if(openflag[j][i].openflag==0)				//若文件未被打开
		cout<<"Read failed.\nThe file has not been opened."<<endl;
	else
	{
		if(openflag[j][i].openmode==1||openflag[j][i].openmode==2)
		{
			itoa(userfile[j]->userAllFile[i].fileAddress,str,10);
			strcpy(str1,"ownfile\\myfile\\file");		//复制地址到路径
			strcat(str1,str);						//追加文件ID

			if(userfile[j]->userAllFile[i].fileLength!=0)
			{
				cout<<"The file has text.Overwrite or append?"<<endl;
				cout<<"(0-overwrite, 1-append)"<<endl;		//选择要覆盖还是追加
				cout<<"choose ";
				cin>>c;

				while(c<0 || c>1)
				{
					cout<<"Input error."<<endl<<endl;
					cout<<"The file has text.Overwrite or append?"<<endl;
					cout<<"(0-overwrite, 1-append)"<<endl;		//选择要覆盖还是追加
					cout<<"choose ";
					cin>>c;
				}

				if(c==0)							//若选择覆盖
				{
					cout<<"Overwrite:";
					fp_file=fopen(str1,"wb");		//以新建方式打开文件
					userfile[j]->userAllFile[i].fileLength=write();	//文件长度为原长度加新写入长度
				}
				else
				{
					cout<<"Append:";
					fp_file=fopen(str1,"ab");		//以新建方式打开文件
					userfile[j]->userAllFile[i].fileLength+=write();	//文件长度为原长度加新写入长度
				}
			}
			else
			{
				fp_file=fopen(str1,"wb");		//以新建方式打开文件
				userfile[j]->userAllFile[i].fileLength=write();	//文件长度为原长度加新写入长度
			}
			fclose(fp_file);						//关闭文件
		}
		else										//若文件不可读
			cout<<"The file cannot be write."<<endl;	//输出提示
	}
	cout<<endl;
}
void quitLogin()
{
	char str[MAXNAME];
	setPathNumber(1);
	int i;

	if(fp_mfd!=NULL)	fclose(fp_mfd);			//关闭所有指针
	if(fp_ufd!=NULL)	fclose(fp_ufd);
	if(fp_file!=NULL)	fclose(fp_file);

	for(int j=0;j<userCount;j++)						//循环所有的用户
	{
		strcpy(str,"ownfile\\");					//取文件路径
		strcat(str,userfile[j]->userName);				//将用户名追加到路径后
		fp_ufd=fopen(str,"wb");					//以只写模式打开
		fclose(fp_ufd);							//关闭文件
		fp_ufd=fopen(str,"ab");					//以追加方式打开文件
		for(i=0;i<fileCount[j];i++)				//循环所有的文件
			fwrite(&userfile[j]->userAllFile[i],sizeof(file),1,fp_ufd);	//写入用户文件中
		fclose(fp_ufd);							//关闭文件
	}		
}

void helpFile()                                 //帮助函数
{	
	cout<<"dir -------- 浏览当前目录下所有文件"<<endl;	//打印帮助信息
	cout<<"create ----- 创建文件"<<endl;
	cout<<"delete ----- 删除文件"<<endl;
	cout<<"open ------- 打开文件"<<endl;
	cout<<"close ------ 关闭文件"<<endl;
	cout<<"read ------- 阅读文件"<<endl;
	cout<<"write ------ 写文件"<<endl;
	//cout<<"modify ----- 修改文件模式"<<endl;
	cout<<"help ------- 打印帮助"<<endl;
	cout<<"quit ------- 用户登出"<<endl<<endl;
}

void chooseCommand()
{
	char choice[6];                                         //记录文件的操作
	while(strcmp(choice,"quit")!=0)                         //当操作命令不是quit时,循环执行用户文件的操作
	{
		cout<<"OWNFILE\\"<<dirname<<"\\";                   //显示当前目录
		cin>>choice;                                        //读入用户输入的文件操作类型
		if (strcmp(choice,"dir")==0) dirFile();             //若与dir匹配,调用显示文件目录的函数
		else if(strcmp(choice,"create")==0) createFile();
		else if(strcmp(choice,"delete")==0) deleteFile();
		else if(strcmp(choice,"open")==0) openFile();
		else if(strcmp(choice,"close")==0) closeFile();
		else if(strcmp(choice,"read")==0) readFile();
		else if(strcmp(choice,"write")==0) writeFile();
		//else if(strcmp(choice,"modigy")==0) modifyFile();
		else if(strcmp(choice,"quit")==0) quitLogin();	
		else if(strcmp(choice,"help")==0) helpFile();
		else cout<<"Bad command."<<endl;                     //输出错误操作的提示
	}
}

void main()
{
	int i;
	int chioce=0;

	if((fp_mfd=fopen("ownfile\\mfd","rb"))==NULL)     //检验mfd文件是否已经存在
	{
		fp_mfd=fopen("ownfile\\mfd","wb");            //如果不存在择新建一个mfd文件
		fclose(fp_mfd);                               //关闭mfd文件
	}

	for(i=0;i<MAX;i++) fileAddressno[i]=0;             //将每个文件的物理地址数初始化为0

	while(chioce!=3)                                   //循环执行操作,直到用户选择3退出循环
	{
		cout<<"Enter your choice"<<endl;               //输出提示信息
		cout<<"1:Login"<<endl;
		cout<<"2.Create new user"<<endl;
		cout<<"3:Exit\n"<<endl;
		cin>>chioce;                                   //读入选择数
		if(chioce==1)                                  //选择1表示用户登录
		{
			loginFile();                               //调用用户登录函数,判断用户是否合法
			if(loginSucceed==1)                       //如果登录成功,用户则可以进行文件系统的相关操作
				chooseCommand();
		}
		else if(chioce==2) 	createNewUser();           //选择2调用生成新用户函数
		else if(chioce==3) break;                      //选择3退出
		else cout<<"Wrong enter!"<<endl;               //选择其他数输出错误提示
		cout<<endl;
	}
}

⌨️ 快捷键说明

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