#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;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
设计一个简单的多用户文件系统。①在系统中用一个文件来模拟一个磁盘;②此系统至少有:Create、delete、open、close、read、write等和部分文件属性的功能。③实现这个文件系统。④能实际演示这个文件系统。 (1)程序采用二级文件目录(即设置主目录MFD)和用户文件目录(UFD)。另外,为打开文件设置了运行文件目录(AFD)。 (2)为了便于实现,可以对文件读写作进行简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。 (3)文件目录的检索使用简单的线性搜索即可。 (4)其他自行设定的要求条件。
资源推荐
资源详情
资源评论
收起资源包目录
代码.zip (12个子文件)
代码
FILEEXP.layout 737B
bin
Debug
FILEEXP.exe 1019KB
FILEEXP.depend 429B
User.h 395B
obj
Debug
main.o 54KB
main.exe 1.85MB
UFD.h 1KB
MFD.h 259B
File.h 2KB
main.cpp 5KB
FILEEXP.cbp 1KB
User.h.gch 6.74MB
共 12 条
- 1
资源评论
lemonsodaboy
- 粉丝: 4
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功