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

📄 文件管理.cpp

📁 操作系统实验和实验报告
💻 CPP
字号:
#include<stdio.h>
#include <stdlib.h> 
#include <conio.h> 
#include<string.h>

struct ufd{//文件主目录块
	char filename[10];
	char password[10];
	char filelength[10];
	struct ufd  *next;
}*file,*p,*pr;

struct mdf{//用户目录块
	char username[10];
	struct ufd  *link;
}user[10]={{"one",NULL},{"two",NULL},{"three",NULL},{"four",NULL},
{"five",NULL},{"six",NULL},{"seven",NULL},{"eight",NULL},
{"nine",NULL},{"ten",NULL}};

struct afd{//打开目录块
	char openname[10];
	char openpassword[10];
	struct afd  *next;
}*open,*openpr;

char *cmd;

void initfile(){
	int i;
	p=(ufd*)malloc(sizeof(ufd));//user1
	strcpy(p->filename,"user1"); 
	strcpy(p->password,"123");  
	strcpy(p->filelength,"20"); 
	p->next=NULL;
	pr=p;
	file=pr;
	//user2
	p=(ufd*)malloc(sizeof(ufd));
	strcpy(p->filename,"user2"); 
	strcpy(p->password,"123");  
	strcpy(p->filelength,"20"); 
	p->next=NULL;
	pr->next=p;
	pr=pr->next;
	//user3
	p=(ufd*)malloc(sizeof(ufd));
	strcpy(p->filename,"user3"); 
	strcpy(p->password,"123");  
	strcpy(p->filelength,"20"); 
	p->next=NULL;
	pr->next=p;
}

void createfile(){
	ufd *q;
	int n=1;
	for(pr=file;pr->next!=NULL;pr=pr->next) n++;
	if(n!=10){
		p=(ufd*)malloc(sizeof(ufd));
		printf("\n请输入要创建的文件名:");
		scanf("%s",p->filename);
		q=file;
		while(q!=NULL){
			for(q=file;q!=NULL;q=q->next){
				if(strcmp(q->filename,p->filename)) continue;
				if(!strcmp(q->filename,p->filename)){
					printf("\n已存在该文件名,请重新输入要创建的文件名:");
					scanf("%s",p->filename);
					q=file;
					continue;
				}
			}
		}
		printf("\n请输入要创建的文件保护码:");
		scanf("%s",p->password);
		strcpy(p->filelength,"20"); 
		p->next=NULL;
		pr->next=p;
		printf("\n文件创建成功!\n");
	}
	else{
		printf("\n文件个数超出10个!不允许再创建!\n");
	}
}

void deletefile(){
	ufd *q;
	q=(ufd*)malloc(sizeof(ufd));
	int n=0;
	if(file!=NULL){
		printf("\n请输入要删除的文件名:");
		scanf("%s",q->filename);
		if(!strcmp(q->filename,file->filename)){
			file=file->next;
			n=1;
		}
		else{
			for(pr=file;pr->next!=NULL;){
				if(!strcmp(q->filename,pr->next->filename)){
					pr->next=pr->next->next;
					n=1;
					continue;
				}
				pr=pr->next;
			}
		}
		if(n==1) printf("\n删除成功\n");
		if(n==0) printf("\n不存在该文件名\n");
	}
	else printf("\n当前用户的文件已全部被删除!\n");
}


void outputopen(){//输出打开文件目录
	printf("\n已打开文件目录如下:\n");
	printf("\n 文件名        保护码\n");
	for(openpr=open;openpr!=NULL;openpr=openpr->next){
		printf("\n  %-11s ",openpr->openname);
		printf("  %-11s \n",openpr->openpassword);
	}
}

void openfile(){//打开文件程序
	afd *q;
	int i=0;
	q=(afd*)malloc(sizeof(afd));
	q->next=NULL;
	printf("\n请输入要打开的文件名:");
	scanf("%s",q->openname);
	for(pr=file;pr!=NULL;pr=pr->next){
		if(!strcmp(q->openname,pr->filename)){
			i=1;
			break;
		}
	}
	if(open!=NULL){
		for(openpr=open;openpr!=NULL;openpr=openpr->next){
			if(!strcmp(q->openname,openpr->openname)) i=2;
		}
	}
	if(i==1){
		printf("\n请输入要打开文件的保护码:");
		scanf("%s",q->openpassword);
		if(!strcmp(q->openpassword,pr->password)){
			printf("\n已成功打开文件!\n");
			if(open==NULL)	open=q;
			else{
				for(openpr=open;openpr->next!=NULL;openpr=openpr->next);
				openpr->next=q;
			}
			outputopen();
		}
		else{
			printf("\n保护码输入错误!\n");
		}
	}
	else if(i==2) printf("\n该文件已经打开!\n");
	else 	      printf("\n没有你要打开的文件或已被删除!\n");
}

void closefile(){
	afd *q;
	int count=0;
	q=(afd*)malloc(sizeof(afd));
	if(open==NULL) printf("\n没有打开的文件!\n");
	else{
		printf("\n请输入要关闭的文件名:");
		scanf("%s",q->openname);
		if(!strcmp(q->openname,open->openname)){
			open=open->next;
			count=1;
			printf("\n该文件已经成功关闭!\n");
		}
		else{
			for(openpr=open;openpr->next!=NULL;){
				if(!strcmp(q->openname,openpr->next->openname)){
					openpr->next=openpr->next->next;
					count=1;
					printf("\n该文件已经成功关闭!\n");
				}
				else openpr=openpr->next;
			}
		}
		if(count==0)	printf("\n没有打开该文件或不存在该文件!\n");
		if(open==NULL)	printf("\n所有文件都已经关闭!\n");
		if(open!=NULL)	outputopen();
	}
}

void readfile(){
	afd *q;
	int n;
	q=(afd*)malloc(sizeof(afd));
	if(open==NULL) printf("\n没有打开文件!不能读,请先打开文件!\n");
	else{
		printf("\n请输入要阅读的文件名:");
		scanf("%s",q->openname);
		for(openpr=open;openpr!=NULL;openpr=openpr->next){
			if(!strcmp(q->openname,openpr->openname)) n=2;
		}
		if(n==2){
			for(pr=file;pr!=NULL;pr=pr->next){
				if(!strcmp(q->openname,pr->filename)){
					printf("\n 文件名        保护码       文件长度 \t\n");
					printf("\n  %-11s ",pr->filename);
					printf("  %-11s ",pr->password);
					printf("  %-11s\n",pr->filelength);
				}
			}
		}
		else{
			printf("\n没有打开该文件!不能读,请先打开该文件!\n");
		}
	}
}

void writefile(){
	afd *q;
	int n,len;
	q=(afd*)malloc(sizeof(afd));
	if(open==NULL) printf("\n没有打开文件!不能写,请先打开文件!\n");
	else{
		printf("\n请输入要写入的文件名:");
		scanf("%s",q->openname);
		for(openpr=open;openpr!=NULL;openpr=openpr->next){
			if(!strcmp(q->openname,openpr->openname)) n=2;
		}
		if(n==2){
			for(pr=file;pr!=NULL;pr=pr->next){
				if(!strcmp(q->openname,pr->filename)){
					printf("\n 文件名        保护码       文件长度 \t\n");
					printf("\n  %-11s ",pr->filename);
					printf("  %-11s ",pr->password);
					printf("  %-11s\n",pr->filelength);
					printf("\n请输入写入文件长度:");
					scanf("%s",pr->filelength);
					printf("\n写入成功!\n");
					printf("\n 文件名        保护码       文件长度 \t\n");
					printf("\n  %-11s ",pr->filename);
					printf("  %-11s ",pr->password);
					printf("  %-11s\n",pr->filelength);
				}
			}
		}
		else{
			printf("\n没有打开该文件!不能读,请先打开该文件!\n");
		}
	}
}

void outputfile(){
	printf("\n 文件名        保护码       文件长度 \t\n");
	for(p=file;p!=NULL;p=p->next){
		printf("\n  %-11s ",p->filename);
		printf("  %-11s ",p->password);
		printf("  %-11s\n",p->filelength);
	}
}

void admin(){
	int i;
	cmd=(char*)malloc(sizeof(char));
	printf("\n请输入用户名:");
	scanf("%s",cmd);
	for(i=0;;i++){
		for(i=0;i<10;i++){
			if(!strcmp(cmd,user[i].username))  break;
		}
		if(i==10){
			printf("\n无此用户文件!\n\n");
			printf("请输入用户名:");
			scanf("%s",cmd);
			continue;
		}
		break;
	}
}

main(){
	int n=0;
	admin();
	initfile();
	printf("\n当前用户文件目录如下:\n");
	outputfile();
	while(strcmp(cmd,"bye")){
		printf("\n请输入操作命令:");
		scanf("%s",cmd);
		if(!strcmp(cmd,"create")){
			createfile();
			continue;
		}
		if(!strcmp(cmd,"delete")){
			deletefile();
			continue;
		}
		if(!strcmp(cmd,"open")){
			for(openpr=open;openpr!=NULL;openpr=openpr->next) n++;
			if(n==5)printf("\n打开的文件已经达到5个,不允许再打开文件!\n");
			else openfile();
			n=0;
			continue;
		}
		if(!strcmp(cmd,"close")){
			closefile();
			continue;
		}
		if(!strcmp(cmd,"read")){
			readfile();
			continue;
		}
		if(!strcmp(cmd,"write")){
			writefile();
			continue;
		}
		if(!strcmp(cmd,"bye"));
		else printf("\n无此操作命令!\n");
	}
	if(file==NULL) printf("\n当前用户没有保存任何文件!\n\n");
	else{
		printf("\n当前用户保存的文件目录如下:\n");
		outputfile();
	}
	printf("\n");
}

⌨️ 快捷键说明

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