数据结构项目——操作系统文件管理的设计与实现

该项目是一个基于链表和文件知识实现的操作系统文件管理系统。用户可以创建、打开、删除、关闭文件,系统通过初始化、读写文件操作来管理文件信息。在Linux环境下,由于没有conio.h库,使用getchar替代getch进行输入处理。项目帮助用户便捷地管理和操作文件,同时也复习了数据结构和LinuxC的相关知识。
摘要由CSDN通过智能技术生成

一、项目背景

        操作系统是一个介于软件和硬件之间的特殊硬件,一方面分配硬件资源另一方面支持软件在它上面的实现,对计算机系统资源实施管理,现如今大数据、云计算技术的发展,已经让我们从IT时代进入了DT时代,对数据的存储和利用越来越得到大家的重视,文件资料和档案作为日常工作中的数据也同样存在着存储和利用的问题,因此用于文件档案管理的软件系统应运而生。

二、项目简介

        在现代计算机系统中,要用到大量的程序和数据,由于内存容量有限,且不能长期保存,故而平时总是把他们以文件的形式存放在外存中,需要时可随时将它们调入内存。如果由用户直接管理外存上的文件,不仅要求用户熟悉外存特性,还必须能保持数据的安全性和一直性。显然,这是用户所不能胜任的工作.取而代之的,便是在操作系统中增加了文件管理功能,即构成一个文件系统,负责管理在外存上的文件,并把对文件的存取,共享和保护等手段提供给用户。这不仅方便了用户,保证了文件的安全性,还有效的提高了系统资源利用率。

        本项目操作系统文件管理的设计与实现,使用链表及文件知识,完成用户创建成功进行文件的新建、打开、删除、关闭、读、写、显示等操作。

三、项目实现功能

        首先系统要完成初始化的任务,建立起整个系统,输入管理员密码,进行用户创建,创建成功后,用户登录模块,对用户的用户名进行验证,如果用户登录成功,则系统进入等待用户输入的状态,用户选择相应指令后,系统按照即定方式处理用户请求,用户退出后,系统转入登录模块,等待下一位用户的登录。

四、项目中遇到的问题有哪些?如何解决?学到了什么?

        Linux系统下使用getch时没有conio.h库,因为conio.h 不是标准库函数,使用getchar时遇到的问题导致输入管理员密码错误。

        此次项目不仅使数据结构链表更加熟练掌握,更复习到LinuxC相关的文件知识,温故而知新。

//head.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
	char password[10];
}Admin;

typedef struct node1{
	char userName[10];
	struct node1 *next;
}User;

typedef struct node2{
	char fileName[10];
	int update; //修改标志
	int flag;   //打开标志
	int record;
	struct node2 *next;
}FileInfo;

typedef struct node3{
	int recordId;//唯一
	char name[10];
	char sex[2];
	struct node3 *next;
}Record;

char userName[10];

char *newStrcat(char *,char *,char *);
int landUser();
int landAdmin();
void exitUser(char *);
void Initialize();
void saveInfo(FileInfo *,char *);
void writeInfo(FileInfo *,char *);
FileInfo *readInfo(char *);
int judgeFileOpen(FileInfo *,char *);
int judgeFileName(FileInfo *,char *);
void newFile(char *);
void deleteFile(char *);
void openFile(char *);
void closeFile(char *);
void scanUsers();
void displayDirectory(char *);
void displayRecord(char *);
void firstInterface();
void usersInterface();
void writeInterface();
void endInterface();
void systemOption();
void usersOption();
void writeOption();
Record *readRecord(char *,char *);
void searchRecord(char *);
Record *newRecord();
void saveRecord(Record *,char *);
void writeRecord(Record *,char *,char *);
void updateRecordCount(char *,char *,int);
void addRecord(char *);
void deleteRecord(char *);
User *createUser();
int judgeUserName(char *);
void saveUser(User *);
void addUser();
User *readUser();
void deleteUser();

//doMain.c
#include "../include/head.h"


char *newStrcat(char *dir,char *userName,char *fileName){
	strcat(dir,userName);
	strcat(dir,"/");
	strcat(dir,fileName);
	strcat(dir,".txt");
	return dir;
}

int landUser(){
	char name[10];
	printf("\t\t登录用户名:");
	scanf("%s",name);
	if(judgeUserName(name)==0){
		strcpy(userName,name);
		printf("\n\t\t登录成功!\n");
		return 1;
	}
	return 0;
}

void exitUser(char *userName){
	FileInfo *file=NULL,*p=NULL;
	int flag=0;
	char option;
	file=readInfo(userName);
	if(file!=NULL){
		p=file;
		while(p!=NULL){
			if(p->flag){
				puts("\t\t==============");
				flag++;
				printf("\t\t%s文件是打开的\n",p->fileName);
				puts("\t\t==============");
			}
			p=p->next;
		}
		if(flag){
			printf("\t\t当前还有%d个文件尚未关闭!\n",flag);
			printf("\t\t是否强制退出?(Y/N):");
			scanf("%s",&option);
			switch(option){
			case 'y':
			case 'Y':
				p=file;
				while(p!=NULL){
					if(p->flag){
						p->flag=0;
						p->update=0;//可以先判断再修改
					}
					p=p->next;
				}
				writeInfo(file,userName);//写会info.txt文件
				userName[0]='\0';
				puts("\t\t所有文件被强制关闭,退出用户成功!");
				system("pause");
				systemOption();
				break;
			case 'n':
			case 'N':
			default:
				usersOption();
				break;
			}
		}else systemOption();
	}else systemOption();
}

void Initialize(){
	FILE *fp=NULL;
	char c;
	int pos;
	Admin *user=(Admin *)malloc(sizeof(Admin));
	if((fp=fopen("admin.txt","r"))==NULL){
		puts("\t\t本系统第一次使用,请初始化....");
		system("pause");
		printf("\t\t输入管理员密码:");
		for(c=getchar(),pos=0;c!='\n';c=getchar()){
			if(c=='\b'){
				putchar('\b');
				putchar(' ');
				putchar('\b');
				pos--;
			}else{
				if((c>32)&&(c<126)){
					putchar('*');
					(user->password)[pos++]=c;
				}else{
					puts("\t\t非法字符输入!");
					Initialize();
				}
			}
		}
        (user->password)[pos]='\0';
		fp=fopen("admin.txt","w");
		fwrite(user,sizeof(Admin),1,fp);
		fclose(fp);
	}else return;
}

int landAdmin(){
	char password[10],c;
	int pos;
	FILE *fp=NULL;
	Admin *admin=NULL;
	admin=(Admin *)malloc(sizeof(Admin));
	fp=fopen("admin.txt","r");
	fread(admin,sizeof(Admin),1,fp);
	fclose(fp);
	printf("\t\t输入管理员密码:");
	for(c=getchar(),pos=0;c!='\n';c=getchar()){
		if(c=='\b')
		{
			putchar('\b');
			putchar(' ');
			putchar('\b');
			pos--;
		}else{
			if((c>32)&&(c<126)){
				putchar('*');
				password[pos++]=c;
			}else{
				puts("\t\t非法字符输入!");
			}
		}
	}
	getchar();
	password[pos]='\0';
	if(!strcmp(password,admin->password)) return 1;//正确返回1,错误返回0
	else{
		puts("\t\t密码错误!");
		system("pause");
		return 0;
	}
}

int main(){
	Initialize();
	systemOption();
	return 0;
}
//fileOperate.c
#include "../include/head.h"

void saveInfo(FileInfo *head,char *dir){
	FILE *fp=NULL;
	FileInfo *p=NULL;
	fp=fopen(dir,"w");
	if(head==NULL){
		fclose(fp);
		return;
	}else{
		p=head;
		while(p!=NULL){
			fwrite(p,sizeof(FileInfo),1,fp);
			p=p->next;
		}
	}
	fclose(fp);
}

void writeInfo(FileInfo *head,char *userName){
	FILE *fp=NULL;
	char dir[20]={"\0"};
	FileInfo *p=NULL;
	newStrcat(dir,userName,"info");
	mkdir(userName);
	saveInfo(head,dir);
}

FileInfo *readInfo(char *userName){
	FILE *fp=NULL;
	FileInfo *head=NULL,*p=NULL,*q=NULL;
	char dir[20]={"\0"};
	newStrcat(dir,userName,"info");
	fp=fopen(dir,"r");
	p=q=(FileInfo *)malloc(sizeof(FileInfo));
	if(fread(p,sizeof(FileInfo),1,fp)==1) head=p;
	while(!feof(fp)){
		q=(FileInfo *)malloc(sizeof(FileInfo));
		if(fread(q,sizeof(FileInfo),1,fp)!=1) break;               
		p->next=q; 
		p=q; 
	}
	p->next=NULL;
	fclose(fp);	
	return head;
}

int judgeFileOpen(FileInfo *head,char *fileName){
	FileInfo *p=NULL;
	if(head==NULL){
		return 0;
	}else{
		p=head;
		while(p!=NULL){
			if(!strcmp(p->fileName,fileName)) return p->flag;
			p=p->next;
		}
		return 0;
	}
}

int judgeFileName(FileInfo *head,char *fileName){
	//返回1,没有重名;返回0,有重名
	FileInfo *p=NULL;
	if(head==NULL){
		return 1;
	}else{
		p=head;
		while(p!=NULL){
			if(!strcmp(p->fileName,fileName)) return 0;
			p=p->next;
		}
		return 1;
	}
}

void newFile(char *userName){
	FILE *fp=NULL;
	char fileName[10];
	char dir[20]={"\0"};
	User *user=NULL;
	FileInfo *head=NULL,*p=NULL,*newFile=NULL;
	displayDirectory(userName);
	printf("\t\t输入新建文件名:");
	scanf("%s",fileName);
	head=readInfo(userName);
	if(judgeFileName(head,fileName)){
		newFile=(FileInfo *)malloc(sizeof(FileInfo));
		strcpy(newFile->fileName,fileName);
		newFile->update=0;
		newFile->flag=0;
		newFile->record=0;
		newFile->next=NULL;
		if(head==NULL) head=newFile;
		else{
			p=head;
			while(p->next!=NULL) p=p->next;
			p->next=newFile;
		}
		writeInfo(head,userName);
	}else{
		puts("\t\t已存在此名称的的文件!\n");
		system("pause");
		return;
	}
	mkdir(userName);
	newStrcat(dir,userName,fileName);
	fp=fopen(dir,"w");
	fclose(fp);
	puts("\t\t创建文件成功!");
	system("pause");
	displayDirectory(userName);
}

void deleteFile(char *userName){
	FILE *fp=NULL;
	char fileName[10];
	char dir[20]={"\0"};
	char flag;
	User *user=NULL;
	FileInfo *head=NULL,*p=NULL,*q=NULL;
	
	printf("\t\t输入要删除的文件名:");
	scanf("%s",fileName);
	head=readInfo(userName);
	if(!judgeFileName(head,fileName)){
		if(!strcmp(head->fileName,fileName)){
			head=head->next;
		}else{
			p=head;
			q=p->next;
			while(q!=NULL){
				if(!strcmp(q->fileName,fileName)){
					if(q->flag==1){
						puts("\t\t文件处于打开状态,请确认后删除...");
						system("pause");
						printf("\t\t确认删除?(Y/N):");
						scanf("%c",&flag);
						switch(flag){
						case 'Y':
						case 'y':
							p->next=q->next;
							break;
						case 'N':
						case 'n':
						default:
							break;
						}
					}else p->next=q->next;
					break;
				}
				p=q;
				q=p->next;
			}
		}
	}else{
		puts("\t\t不存在此名称的的文件!");
		system("pause");
		return;
	}
	writeInfo(head,userName);//重写info.txt文件
	newStrcat(dir,userName,fileName);
	remove(dir);
	puts("\t\t删除文件成功!");
	system("pause");
}


void openFile(char *userName){
	FileInfo *file=NULL,*head=NULL;
	char fileName[10];
	printf("\t\t输入要打开的文件名:");
	scanf("%s",fileName);
	head=file=readInfo(userName);
	if(head==NULL){
		puts("\t\t该用户无文件!\n");
		system("pause");
		return;
	}
	while(file!=NULL){
		if(!strcmp(file->fileName,fileName)){
			if(file->flag==1){
				puts("\t\t文件已经处于打开状态!");
				system("pause");
				break;
			}else{
				file->flag=1;
				writeInfo(head,userName);
				puts("\t\t文件被打开!");
				system("pause");
				return;
			}
		}
		file=file->next;
	}
	if(file==NULL) puts("\t\t该文件名不存在!");
}

void closeFile(char *userName){
	FileInfo *file=NULL,*head=NULL;
	char fileName[10];
	printf("\t\t输入要关闭的文件名:");
	scanf("%s",fileName);
	head=file=readInfo(userName);
	if(head==NULL){
		puts("\t\t该用户无文件!");
		system("pause");
		return;
	}
	while(file!=NULL){
		if(!strcmp(file->fileName,fileName)){
			if(file->flag==0){
				puts("\t\t文件已经处于关闭状态!");
				system("pause");
				break;
			}else{
				file->flag=0;
				file->update=0;
				writeInfo(head,userName);
				puts("\t\t文件被关闭!");
				system("pause");
				return;
			}
		}
		file=file->next;
	}
	if(file==NULL) puts("\t\t该文件名不存在!");
}

void displayDirectory(char *userName){
	FileInfo *p=NULL;
	FILE *fp=NULL;
	char dir[20]={"\0"};
	newStrcat(dir,userName,"info");
	if((fp=fopen(dir,"r"))==NULL){
		printf("\n\t\t目录%s不存在!\n",userName);
		return;
	}
	p=readInfo(userName);
	printf("\t\t=======================================\n");
	printf("\t\t            %s目录            \n",userName);
	printf("\t\t=======================================\n");
	printf("\t\t= 文件名   修改位   打开标志   记录数 =\n");
	while(p!=NULL){
		printf("\t\t  %s          %d         %d         %d\n",p->fileName,p->update,p->flag,p->record);
		p=p->next;
	}
	printf("\t\t=======================================\n");
	fclose(fp);
}

//menu.c
#include "../include/head.h"

void firstInterface(){
	puts("");
	printf("\t\t=============================================\n");
	printf("\t\t=          WELCOME TO USE FILE SYSTEM       =\n");
	printf("\t\t=============================================\n");
	printf("\t\t=         1--Scan_users(扫描查找用户)       =\n");
	printf("\t\t=         2--Add_user(添加用户)             =\n");
	printf("\t\t=         3--Log_user(记录用户)             =\n");
	printf("\t\t=         4--Delete_user(删除用户)          =\n");
	printf("\t\t=         0--Exit_system(退出系统)          =\n");
	printf("\t\t=============================================\n");
}

void usersInterface(){
	puts("");
	printf("\t\t=============================================\n");
	printf("\t\t=                 FILE SYSTEM               =\n");
	printf("\t\t=============================================\n");
	printf("\t\t=   1--New_file           2--Open_file      =\n");
	printf("\t\t=   3--Delete_file        4--Close_file     =\n");
	printf("\t\t=   5--Read_file          6--Write_file     =\n");
	printf("\t\t=   7--Display_directory  8--Dispaly_file   =\n");
	printf("\t\t=   9--Close_user         0--Back_last      =\n");
	printf("\t\t=============================================\n");
}

void writeInterface(){
	puts("");
	printf("\t\t=============================================\n");
	printf("\t\t=               FILE SYSTEM                 =\n");
	printf("\t\t=============================================\n");
	printf("\t\t=              1--Add_record                =\n");
	printf("\t\t=              2--Delete_record             =\n");
	printf("\t\t=              0--Back_last                 =\n");
	printf("\t\t=============================================\n");
}

void endInterface(){
	puts("");
	printf("\t\t=============================================\n");
	printf("\t\t=               THANK  YOU                  =\n");
	printf("\t\t=============================================\n");

}

void systemOption(){
	int option;
	do{
		firstInterface();
		printf("\t\tCHOOSE ITEM:");
		scanf("%d",&option);
		switch(option){
			case 1:
				scanUsers();
				break;
			case 2:
				if(landAdmin()) addUser();
				break;
			case 3:
				if(landUser()) usersOption();
				else{
					puts("\t\t登录失败!");
					system("pause");
				}
				break;
			case 4:
				if(landAdmin()) deleteUser();
				break;
			case 0:
				system("cls");
				endInterface();
				exit(0);
			default:
				break;
		}
	}while(option!=0);
	
}

void usersOption(){
	int option;
	do{
		usersInterface();
		printf("\t\tCHOOSE ITEM:");
		scanf("%d",&option);
		switch(option){
			case 1:
				newFile(userName);
				break;
			case 2:
				openFile(userName);
				break;
			case 3:
				deleteFile(userName);
				break;
			case 4:
				closeFile(userName);
				break;
			case 5:
				searchRecord(userName);
				break;
			case 6:
				system("cls"); 
				writeOption();
				break;
			case 7:
				displayDirectory(userName);
				break;
			case 8:
				displayRecord(userName);
				break;
			case 9:
				exitUser(userName);
				break;
			case 0:
				system("cls");
				exitUser(userName);
				systemOption();
				system("pause");
				exit(0);
			default:
				break;
		}
	}while(option!=0);
}

void writeOption(){
	int option;
	do{
		writeInterface();
		printf("\t\tCHOOSE ITEM:");
		scanf("%d",&option);
		switch(option){
			case 1:
				addRecord(userName);
				break;
			case 2:
				deleteRecord(userName);
				break;
			case 0:
				system("cls");
				usersOption();
				system("pause");
				exit(0);
			default:
				break;
		}
	}while(option!=0);
}
//recordOperate.c
#include "../include/head.h"

Record *readRecord(char *userName,char *fileName){
	FILE *fp=NULL;
	Record *head=NULL,*p=NULL,*q=NULL;
	char dir[20]={"\0"};
	newStrcat(dir,userName,fileName);
	fp=fopen(dir,"r");
	p=q=(Record *)malloc(sizeof(Record));
	if(fread(p,sizeof(Record),1,fp)==1) head=p;
	while(!feof(fp)){
		q=(Record *)malloc(sizeof(Record));
		if(fread(q,sizeof(Record),1,fp)!=1) break;               
		p->next=q; 
		p=q; 
	}
	p->next=NULL;
	fclose(fp);	
	return head;
} 

void searchRecord(char *userName){
	int id=-1;
	Record *head=NULL;
	FileInfo *file=NULL;
	char fileName[10];
	printf("\t\t输入要显示记录的文件名:");
	scanf("%s",fileName);
	file=readInfo(userName);
	if(judgeFileOpen(file,fileName)==0){
		puts("\t\t文件未打开!\n");
		system("pause");
		return;
	}
	puts("\t\t输入记录号:");
	scanf("%d",&id);
	head=readRecord(userName,fileName);
	if(head==NULL){
		puts("\t\t文件无记录!\n");
		system("pause");
		return;
	}
	puts("\n\t\t================================\n");
	puts("\t\t\t记录号\t姓名\t性别");
	while(head!=NULL){
		if(head->recordId==id){
			printf("\t\t\t%d\t%s\t%s\n",head->recordId,head->name,head->sex);
			break;
		}
		head=head->next;
	}
	if(head==NULL) puts("\t\t没有该记录号的记录信息!\n");
	puts("\n\t\t================================\n");
	system("pause");
}

Record *newRecord(){
	Record *newOne=NULL;
	newOne=(Record *)malloc(sizeof(Record));
	printf("\t\t姓名:");
	scanf("%s",newOne->name);
	printf("\t\t性别(M/F):");
	scanf("%s",newOne->sex);
	newOne->recordId=0;
	newOne->next=NULL;
	return newOne;
}

void saveRecord(Record *head,char *dir){
	FILE *fp=NULL;
	fp=fopen(dir,"w");
	if(head==NULL){
		fclose(fp);
		return;
	}
	while(head!=NULL){
		fwrite(head,sizeof(Record),1,fp);
		head=head->next;
	}
	fclose(fp);
}

void writeRecord(Record *head,char *userName,char *fileName){
	FILE *fp=NULL;
	char dir[20]={"\0"};
	newStrcat(dir,userName,fileName);
	saveRecord(head,dir);
}

void updateRecordCount(char *userName,char *fileName,int count){
	FileInfo *file=NULL,*p=NULL;
	file=readInfo(userName);
	p=file;
	if(p==NULL) return;
	while(p!=NULL){
		//记录数+/-1,根据count来确定
		if(!strcmp(p->fileName,fileName)){
			p->record=p->record+count;
			p->update=1;//文件被修改
		}
		p=p->next;
	}
	writeInfo(file,userName);
}

void addRecord(char *userName){
	Record *newOne=NULL;
	Record *head=NULL,*p=NULL;
	FileInfo *file=NULL;
	char fileName[10];
	printf("\t\t输入要添加记录的文件名:");
	scanf("%s",fileName);
	file=readInfo(userName);
	while(file!=NULL){
		if(!strcmp(file->fileName,fileName)){
			if(file->flag==0){
				puts("\t\t文件未打开!\n");
				return;
			}else break;
		}
		file=file->next;
	}
	if(file==NULL){
		puts("\t\t该文件不存在!\n");
		system("pause");
		return;
	}
	head=readRecord(userName,fileName);
	if(head==NULL){
		head=newRecord();
	}else{
		newOne=newRecord();
		p=head;
		while(p->next!=NULL) p=p->next;
		newOne->recordId=p->recordId+1;//实现记录号自动加1
		p->next=newOne;
	}
	writeRecord(head,userName,fileName);
	updateRecordCount(userName,fileName,1);//对文件的操作(修改记录数等)
	puts("\t\t添加记录成功!");
	system("pause");
}

void deleteRecord(char *userName){
	Record *head=NULL,*p=NULL,*q=NULL;
	FileInfo *file=NULL;
	int id=-1;
	char fileName[10];
	printf("\t\t输入要删除记录的文件名:");
	scanf("%s",fileName);
	file=readInfo(userName);
	while(file!=NULL){
		if(!strcmp(file->fileName,fileName)){
			if(file->flag==0){
				puts("\t\t文件未打开!\n");
				return;
			}else break;
		}
		file=file->next;
	}
	if(file==NULL){
		puts("\t\t该文件不存在!\n");
		system("pause");
		return;
	}
	head=readRecord(userName,fileName);
	if(head==NULL){
		puts("\t\t文件无记录!\n");
		system("pause");
		return;
	}else{
		//也可以显示该文件的所有记录--调用display();
		printf("\t\t输入要删除的记录号:");
		scanf("%d",&id);
		head=readRecord(userName,fileName);
		if(head->recordId==id) head=head->next;
		else{
			p=head;
			q=p->next;
			while(q!=NULL){
				if(q->recordId==id){
					p->next=q->next;
					break;
				}
				p=q;
				q=p->next;
			}
			if(q==NULL){
				puts("\t\t没有该记录号的记录!\n");
				return;
			}
		}
	}
	writeRecord(head,userName,fileName);
	updateRecordCount(userName,fileName,-1);//对文件的操作(修改记录数等)
	puts("\t\t删除记录成功!\n");
}

void displayRecord(char *userName){
	Record *p=NULL;
	FileInfo *file=NULL;
	FILE *fp=NULL;
	char dir[20]={"\0"};
	char fileName[10];
	printf("\t\t输入文件名:");
	scanf("%s",fileName);
	newStrcat(dir,userName,fileName);
	fp=fopen(dir,"r");
	if(fp==NULL){
		printf("\t\t文件%s不存在!\n",fileName);
		fclose(fp);
		return;
	}
	file=readInfo(userName);
	while(file!=NULL){
		if(!strcmp(file->fileName,fileName)){
			if(file->flag==0){
				puts("\t\t文件未打开!\n");
				return;
			}else break;
		}
		file=file->next;
	}
	if(file==NULL){
		puts("\t\t该文件不存在!\n");
		system("pause");
		return;
	}
	p=readRecord(userName,fileName);
	if(p==NULL){
		puts("\t\t没有记录!");
		fclose(fp);
		system("pause");
		return;
	}
	printf("\t\t===================================\n");	
	printf("\t\t           文件%s内容     \n",fileName);	
	printf("\t\t===================================\n");	
	printf("\t\t= 记录号         姓名        性别 =\n");
	while(p!=NULL){
		printf("\t\t  %d            %s         %s\n",p->recordId,p->name,p->sex);
		p=p->next;
	}
	fclose(fp);
}
//userOperate.c
#include "../include/head.h"

User *createUser(){
	char userName[10];
	User *user=NULL;
	printf("\n\t\t输入创建用户名:");
	scanf("%s",userName);
	user=(User *)malloc(sizeof(User));
	strcpy(user->userName,userName);
	user->next=NULL;
	return user;
}

int judgeUserName(char *userName){
	//返回1,没有重名;返回0,有重名
	User *head=NULL,*p=NULL;
	head=readUser();
	if(head==NULL){
		return 1;
	}else{
		p=head;
		while(p!=NULL){
			if(!strcmp(p->userName,userName)) return 0;
			p=p->next;
		}
		return 1;
	}
}

void saveUser(User *head){
	FILE *fp=NULL;
	fp=fopen("users.txt","w");
	while(head!=NULL){
		fwrite(head,sizeof(User),1,fp);
		head=head->next;
	}
	fclose(fp);
}

void addUser(){
	FILE *fp=NULL;
	char userName[10];
	char dir[20]={"\0"};
	User *head=NULL,*p=NULL,*user=NULL;
	user=createUser();
	if(judgeUserName(user->userName)==0){
		puts("\t\t创建用户失败!已有该名称的用户!\n");
		return;
	}else{
		head=readUser();
		if(head==NULL) head=user;
		else{
			p=head;
			while(p->next!=NULL) p=p->next;
			p->next=user;
		}
		newStrcat(dir,user->userName,"info");//info.txt用于记录文件记录数,文件名称,标志位
		mkdir(user->userName);
		fp=fopen(dir,"w");
		fclose(fp);
		saveUser(head);
		printf("\t\t创建用户成功!\n");
		system("pause");
	}
}

User *readUser(){
	FILE *fp=NULL;
	User *head=NULL,*p=NULL,*q=NULL;
	
	if((fp=fopen("users.txt","r"))==NULL) return head;
	else{
		rewind(fp);
		p=q=(User *)malloc(sizeof(User));
		if(fread(p,sizeof(User),1,fp)==1) head=p;
		while(!feof(fp)){
			q=(User *)malloc(sizeof(User));
			if(fread(q,sizeof(User),1,fp)!=1) break;               
			p->next=q; 
			p=q; 
		}
		p->next=NULL;
		fclose(fp);	
	}
	return head;
}

void deleteUser(){
	char userName[10];
	char dir[20]={"\0"};
	User *user=NULL,*p=NULL,*q=NULL;
	int flag=0;
	FileInfo *file=NULL;
	printf("\n\t\t输入要删除的用户名:");
	scanf("%s",userName);
	user=readUser();
	if(!strcmp(user->userName,userName)){
		user=user->next;
		flag=1;
	}else{
		p=user;
		q=p->next;
		while(q!=NULL){
			if(!strcmp(q->userName,userName)){
				p->next=q->next;
				flag=1;
				break;
			}
			p=q;
			q=p->next;
		}
	}
	if(flag){
		saveUser(user);
		file=readInfo(userName);
		while(file!=NULL){
			newStrcat(dir,userName,file->fileName);
			remove(dir);//删除文件
			file=file->next;
		}
		strcpy(dir,userName);
		strcat(dir,"/info.txt");
		remove(dir);	//删除用户名下的info.txt文件
		rmdir(userName);//删除目录
		puts("\t\t删除用户成功!\n");
	}else{
		puts("\t\t该用户不存在!\n");
	}
}

void scanUsers(){
	User *p=NULL;
	FILE *fp=fopen("users.txt","r");
	if(fp==NULL){
		printf("\t\t用户文件不存在!\n");
		system("pause");
		return;
	}else{
		p=readUser();
		puts("\n");
		printf("\t\t============================\n");
		printf("\t\t=      当前已创建用户      =\n");
		printf("\t\t============================\n");		
		while(p!=NULL){
			printf("\t\t\t  %s\n",p->userName);
			p=p->next;
		}
		printf("\t\t============================\n");
		fclose(fp);
	}
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值