1、实验概述
- 实现多用户文件系统,每次用户可保存多个文件,一次运行用户可打开5个文件。
- 实现文件的相关操作,包括创建、删除、打开、关闭以及读写功能。
- 采用二级目录,设置主目录MFD、用户文件目录UFD。
- 设置文件权限保护码:1-可执行,2-只读,3-只写。
- 实现用户登录、退出、退出系统等操作。
- 为简化程序,不对用户信息进行保存,即每次运行,文件系统都相当于初始化一次。
2、结构体设计
(1)用户users
typedef struct users
{
char name[8]; //用户名
char pwd[10]; //密码
}users;
(2)主文件目录MFD
struct MFD{ // 主文件目录
string u_name; // 用户名
int addr; // 指向子目录指针
}mfd[10];
(3)用户文件目录UFD
struct UFD{ // 用户文件目录
File file[1000];
string u_name; // 用户名
int id; // 文件的总数
int cnt; // 用户文件存在的个数
int open_cnt; // 用户打开文件个数,最大为k
void init() ; //初始化数据项
void create() ; //创建用户文件
void del(int pos); //删除用户文件
void open(int pos); //打开文件
void close(int pos); //关闭文件
void read(int pos); //读文件
void write(int pos); //写文件
int Find(string name);//根据文件名查找文件
}ufd[10];
(4)文件File
struct File{
string name; // 文件名
bool exist; // 0表示文件已经删除,1表示文件存在
bool state; // 0表示文件处于关闭状态,1表示打开状态
int protect; // 1表示只执行,2表示只读,3表示只写,4表示可读可写
string content; // 文件内容
void create(); //创建
void del(); //删除
bool open(); //打开
bool close(); //关闭
void read(); //读
void write(); //写
};
3、文件系统的流程图
4、代码实现
(1)User.h
头文件里新加用户信息,用于登录,这种实现方法简单,用户信息固定。
当然,也可以尝试建立一个文件存储用户信息,登陆时将输入的账号密码与文件已有数据比较,只是考虑到这次实验的重点在于体验多用户系统,故而一切从简。
typedef struct users
{
char name[8];
char pwd[10];
}users;
users usrarray[8] =
{
"usr1","usr1",
"usr2","usr2",
"usr3","usr3",
"usr4","usr4",
"usr5","usr5",
"usr6","usr6",
"usr7","usr7",
"usr8","usr8",
};
(2)UFD.h等只是上述的结构体,无追加内容
(3)File.h
struct File{
string name; // 文件名
bool exist; // 0表示文件已经删除,1表示文件存在
bool state; // 0表示文件处于关闭状态,1表示打开状态
int protect; // 1表示只执行,2表示只读,3表示只写,4表示可读可写
string content; // 文件内容
void create();
void del();
bool open();
bool close();
void read();
void write();
};
void File::create(){
cout << "请输入文件名与文件权限(1只执行 2只读 3可写): "; cin >> name >> protect;
cout << "请输入文件内容: "; cin >> content;
exist = 1;
state = 0;
}
void File::del(){
exist = 0;
}
bool File::open(){
if(protect == 1) { cout << "打开失败,该文件为只执行文件!" << endl; return 0;}
else if(state == 1) { cout << "该文件已处于打开状态" << endl; return 0;}
else { state = 1; cout << "打开成功" << endl; return 1;}
}
bool File::close(){
if(state == 0) { cout << "该文件已处于关闭状态" << endl; return 0;}
else { state = 0; cout << "关闭成功" << endl; return 1;}
}
void File::read(){
if(protect == 1) { cout << "读文件失败,该文件为只执行文件!" << endl; }
else { cout << content << endl;}
}
void File::write(){
if(protect==1 || protect==2) { cout << "写文件失败,该文件不可写" << endl;}
else {
string cont; cout << "请输入写入内容: "; cin >> cont;
content=cont; cout << "写入成功!" << endl;
}
}
(4)main.cpp
#include <iostream>
#include<stdlib.h>
#include <cstdio>
#include <cstring>
#include <string>
#include "File.h"
#include "User.h"
using namespace std;
struct MFD{ // 主文件目录
string u_name; // 用户名
int addr; // 指向子目录指针
}mfd[10];
int mfdcnt = 8; // 用户数
int k = 5; // 每个用户最多打开的文件数
struct UFD{ // 用户文件目录
File file[1000];
string u_name; // 用户名
int id; // 文件的总数
int cnt; // 用户文件存在的个数
int open_cnt; // 用户打开文件个数,最大为k
void init() { cnt=0; open_cnt=0; id=0;}
void create() { file[id].create(); cnt++; id++;}
void del(int pos) { file[pos].del(); cnt--;}
void open(int pos) {
if(open_cnt == k) cout<<"您已经打开了"<<k<<"个文件,不能再打开了" << endl;
else {
if(file[pos].open() == 1) open_cnt++;
}
}
void close(int pos) { if(file[pos].close() == 1) open_cnt--; }
void read(int pos) { file[pos].read(); }
void write(int pos) { file[pos].write(); }
int Find(string name) {
int pos = -1; // 需要操作文件的位置
for(int i=0; i<id; i++)
if(file[i].exist==1 && file[i].name==name) return pos=i;
cout << "找不到该文件,请检查文件名\n";
return pos;
}
}ufd[10];
void see(string u_name){
int addr = -1;
for(int i=0; i<mfdcnt; i++){
if(mfd[i].u_name == u_name) addr = mfd[i].addr;
}
if(addr == -1) { cout << "没有该用户" << endl;}
else{
UFD user = ufd[addr];
cout << "********************用户名" << u_name << "********************\n";
cout << "文件个数: " << user.cnt << "\t文件打开数: " << user.open_cnt << endl;
cout << "文件名\t文件状态\t文件权限\t文件内容\n";
for(int i=0; i<user.id; i++){
File f = user.file[i];
if(f.exist == 0) continue;
cout << f.name << "\t";
if(f.state == 0) cout << "关闭\t\t";
else cout << "打开\t\t";
if(f.protect == 1) cout << "只执行\t\t";
else if(f.protect == 2) cout << "只读\t\t";
else if(f.protect == 3) cout << "可写\t\t";
cout << f.content << endl;
}
}
}
void menu(){
printf("************* 操作目录 *****************\n");
printf("************* 0: 退出 *****************\n");
printf("************* 1: 创建文件 *****************\n");
printf("************* 2: 删除文件 *****************\n");
printf("************* 3: 打开文件 *****************\n");
printf("************* 4: 关闭文件 *****************\n");
printf("************* 5: 读文件 *****************\n");
printf("************* 6: 写文件 *****************\n");
printf("************* 7: 查看UFD *****************\n");
printf("************* 8: 退出当前用户 *****************\n");
}
void operate(){
string u_name;
cout << "请输入登入用户名称: "; cin >> u_name;
int u_id = -1;
for(int i=0; i<mfdcnt; i++){
if(mfd[i].u_name == u_name){
while(1){
cout<<"输入密码:"; string pwd; cin>>pwd;
if(pwd == usrarray[i].pwd)
{
cout<<"密码正确,登入成功!"<<endl;
u_id = mfd[i].addr;
break;
}
else{
cout<<"密码错误。"<<endl;
}
}
}
}
if(u_id == -1) { cout << "没有该用户" << endl; return ;}
system("pause");
while(1){
system("cls");
menu();
int op; cin >> op;
if(op == 0) break;
if(op == 1) ufd[u_id].create();
else if(op == 7) see(u_name);
else if(op == 8){
cout << "请输入登入用户名称: "; cin >> u_name;
for(int i=0; i<mfdcnt; i++){
if(mfd[i].u_name == u_name){
while(1){
cout<<"输入密码:"; string pwd; cin>>pwd;
if(pwd == usrarray[i].pwd)
{
cout<<"密码正确,登入成功!"<<endl;
u_id = mfd[i].addr;
break;
}
else{
cout<<"密码错误。"<<endl;
}
}
}
}
}
else {
string name;
cout << "请输入操作文件名: "; cin >> name;
int pos = ufd[u_id].Find(name);
if(pos == -1) continue;
if(op == 2) ufd[u_id].del(pos);
else if(op == 3) ufd[u_id].open(pos);
else if(op == 4) ufd[u_id].close(pos);
else if(op == 5) ufd[u_id].read(pos);
else if(op == 6) ufd[u_id].write(pos);
}
system("pause");
}
}
void InitUser(){
for(int i=0; i<mfdcnt; i++){
mfd[i].u_name = usrarray[i].name; mfd[i].addr = i;
ufd[i].u_name = usrarray[i].name;
}
}
int main(){
cout << "当前用户数m: "; cout<< mfdcnt<<endl;
cout << "用户最多可以打开的文件数: "; cout<<k<<endl;
InitUser();
while(1){
operate();
cout << "还要继续操作吗? (0 or 1): ";
int op; cin >> op; if(op == 0) break;
}
}
4、一些测试截图
(1)登录usr1
(2)在usr1用户目录下创建文件test1,权限为只读;test2,权限为可写
(3)查看usr1用户主目录下文件
(4)切换用户,重复创建文件,查看文件过程
5、总结
本篇博客内容只是对多用户文件系统的一个简单模拟。简易文件系统的功能基本都实现了。在文件系统中,可以进行用户切换、用户登录、在用户文件目录下进行文件的相关操作、显示用户的UFD等。
但是,其实本文件系统还是有提升空间,因为文件系统没有实现信息存储功能,运行结束后,不存储用户信息以及用户的文件信息,每次运行都相当于初始化一遍。
仅供参考,谢谢。