当前位置:   article > 正文

简单的文件系统模拟C/C++(操作系统)_设计一个n个用户的文件系统,每个用户最多可保存m个文件。(2) 限制用户在一次

设计一个n个用户的文件系统,每个用户最多可保存m个文件。(2) 限制用户在一次

一、目的与要求

  1. 目的

文件系统是操作系统的一个重要组成部分,也是与用户关系极为密切的部分。学生应独立的用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程,从而对各种文件操作命令的实质内容和执行过程有比较深入的了解,掌握它们的实施方法,加深对教材中有关内容的理解。‘

  1. 要求
    (1) 设计一个n个用户的文件系统,每个用户最多可保存m个文件。

(2) 限制用户在一次运行中只能打开一个文件。

(3) 系统应能检查输入命令的正确性,出错时要能显示出错原因。

(4) 对文件必须设置保护措施,如只能执行,允许读,允许写等。在每
次打开文件时,根据本次打开的要求,再次设置保护级别,即可有二级保护。

(5) 对文件的操作至少应有下面几条命令:
create 建立文件
delete 删除文件
open 打开文件
close 关闭文件
read 读文件
write 写文件

二、内容

(1) 设计一个共有10个用户的文件系统,每个用户最多可保存10个文件,一次运行过程中用户可同时打开5个文件。

(2) 程序采用二级文件目录,即设置了主文件目录(MFD)和用户文件目录(UFD)。前者应包含文件主(即用户)及他们的目录区指针;后者应给出每个文件主占有的文件目录,即文件名、保护码、文件长度以及它们存放的位置等。另外为打开文件设置了运行文件目录(AFD),在文件打开时应填入打开文件号,本次打开保护码和读写指针等。

(3) 为了便于实现,对文件的读写作了简化,在执行读写命令时,只修改读写指针,并不进行实际文件的读写操作。

主要数据结构:

① 主文件目录(MFD)和用户文件目录(UFD)

这里写图片描述

② 打开文件目录(AFD)

这里写图片描述

三、实现代码

/*
 *模拟文件管理
 */
#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std ;

/*
 *定义文件数据结构
 */
typedef struct file
{
    char file_name[20] ;
    bool file_protect[3] ;
    bool open_file_protect[3] ; //仅在文件打开时有效
    int  read , write ; //定义为读写指针
    int  file_length ;
    struct file *next ;
} File ;

/*
 *用户与文件的映射
 */
typedef struct x_map
{
    char userName[20] ;
    File *file ;
    struct x_map *next ;
} Map ;

/*
 *定义主文件目录
 */
typedef struct mfd
{
    Map *head , *tail ;
} MFD ;

/*
 *打开文件目录
 */
typedef struct afd
{
  File *head , *tail ;
  int max_open ;
  int current_open ;
} AFD ;

/*
 *进行用户的初始化
 */
void initUser(MFD *mfd) ;

/*
 *进行系统用户的输出
 */
void displayUser(MFD *mfd) ;

/*
 *进行用户的查找,找到则返回用户映射指针
 */
Map * queryUser(char userName[] , MFD *mfd) ;

/*
 *进行文件的创建操作
 *成功则返回true , 否则返回false
 */
bool createFile(Map *user , char file_name[] , bool file_protect[3] , int file_length) ;

/*
 *进行文件删除操作
 */
bool deleteFile(Map *user , char file_name[] , AFD *afd) ;

/*
 *进行文件打开操作
 */
bool openFile(Map *user , char file_name[] , AFD *afd , bool open_file_protect[]) ;

/*
 *进行读操作
 */
bool readFile(AFD *afd , char file_name[]) ;

/*
 *进行文件的写操作
 */
bool writeFile(AFD *afd , char file_name[]) ;

/*
 *关闭文件
 */
 bool closeFile(AFD *afd , char file_name[]) ;

/*
 *进行用户文件的显示
 */
void displayUserFile(Map *user) ;

/*
 *显示打开的文件
 */
void displayOpenFile(AFD *afd , Map *user) ;

/*
 *测试主函数
 */
int main()
{
    MFD *mfd ;
    mfd = (MFD*)malloc(sizeof(MFD)) ;
    if(mfd == NULL)
    {
        exit(0) ;
    }
    mfd->head = mfd->tail = NULL ;
    initUser(mfd) ;
    displayUser(mfd) ;

    char userName[20] ;
    while(true)
    {
        cout<<"Please choose user to login : " ;
        cin>>userName ;
        Map *user ;
        user = queryUser(userName , mfd) ;
        if(user == NULL)
        {
            cout<<"No such user ! "<<endl;
        }
        else
        {
          //为用户初始化打开文件目录
          AFD *afd ;
          afd = (AFD*)malloc(sizeof(AFD)) ;
          if(afd == NULL)
          {
              cout<<"The memory is not enough ! "<<endl ;
              exit(0) ;
          }
          afd->head = afd->tail = NULL ;
          afd->max_open = 5 ;
          afd->current_open = 0 ;

          char command[20] ;
          char file_name[20] ;
          bool file_protect[3] ;
          bool open_file_protect[3] ;
          int file_length ;
          while(true)
          {
              cout<<userName<<">>" ;
              cin>>command ;
              //输入命令进行操作
              if(strcmp(command , "create") == 0)
              {
                  cout<<"Please file (file_name file_protect file_length) : " ;
                  cin>>file_name>>file_protect[0]>>file_protect[1]>>file_protect[2]>>file_length ;
                  createFile(user , file_name , file_protect , file_length) ;
                  displayUserFile(user) ;
              }
              else if(strcmp(command , "delete") == 0)
              {
                  cout<<"Please input the file's name you want to delete : " ;
                  cin>>file_name ;
                  deleteFile(user , file_name , afd) ;
                  displayUserFile(user) ;
              }
              else if(strcmp(command , "open") == 0)
              {
                  cout<<"Please input the file name you want to open : " ;
                  cin>>file_name>>open_file_protect[0]>>open_file_protect[1]>>open_file_protect[2] ;
                  openFile(user , file_name , afd , open_file_protect) ;
                  displayOpenFile(afd , user) ;
              }
              else if(strcmp(command , "close") == 0)
              {
                  cout<<"Please input the file you want to close : " ;
                  cin>>file_name ;
                  closeFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(strcmp(command , "read") == 0)
              {
                  cout<<"Please input the file you want to read : " ;
                  cin>>file_name ;
                  readFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(strcmp(command , "write") == 0)
              {
                  cout<<"Please input the file you want to write : " ;
                  cin>>file_name ;
                  writeFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(strcmp(command , "exit") == 0)
              {
                  break ;
              }
              else
              {
                  cout<<"No such command \""<< command <<"\""<<endl ;
              }
          }
        }
    }
    return 0 ;
}

void initUser(MFD *mfd)
{
    //初始化十个不同用户
    for(int i = 1 ; i <= 10 ; i++)
    {
        Map *m ;
        m = (Map*)malloc(sizeof(Map)) ;
        if(m == NULL)
        {
            exit(0) ;
        }
        cout<<"Please input init user name : " ;
        cin>>m->userName ;
        m->file = NULL ;
        m->next = NULL ;
        if(mfd->head == NULL)
        {
            mfd->head = mfd->tail = m ;
        }
        else
        {
            mfd->tail->next = m ;
            mfd->tail = m ;
        }
    }
}

void displayUser(MFD *mfd)
{
    Map *m = NULL ;
    m = mfd->head;
    cout<<"user : " ;
    while(m)
    {
        cout<<m->userName<<" " ;
        m = m->next ;
    }
    cout<<endl ;
}

Map * queryUser(char userName[] , MFD *mfd)
{
    Map *m = NULL ;
    m = mfd->head ;
    while(m)
    {
        if(strcmp(userName , m->userName) == 0)
        {
            return m ;
        }
        m = m->next ;
    }
    return NULL ;
}

bool createFile(Map *user , char file_name[] , bool file_protect[3] , int file_length)
{
    File *file ;
    file = (File*)malloc(sizeof(File)) ;
    if(file == NULL)
    {
        return false ;
    }

    //进行文件的初始化
    strcpy(file->file_name , file_name) ;
    file->file_protect[0] = file_protect[0] ;
    file->file_protect[1] = file_protect[1] ;
    file->file_protect[2] = file_protect[2] ;
    file->file_length = file_length ;
    file->read = file->write = 0 ;
    file->next = NULL ;

    if(user->file == NULL)
    {
        user->file = file ;
    }
    else
    {
       File *op  , *preOp = NULL ;
       op = user->file ;
       //查找是否存在同名文件
       while(op)
       {
           if(strcmp(op->file_name , file->file_name) == 0)
           {
               cout<<"The file name "<<file->file_name<<" is already exit ! "<<endl ;
               return false ;
           }
        preOp = op ;
        op = op->next ;
       }
       preOp->next = file ;
    }
}

void displayUserFile(Map *user)
{
    cout<<"The fileList of "<<user->userName<<endl ;
    File *file = NULL ;
    file = user->file ;
    while(file)
    {
        cout<<file->file_name<<" "<<file->file_protect[0]<<" "<<file->file_protect[1]<<" "<<file->file_protect[2]<<" "<<file->file_length<<endl ;
        file = file->next ;
    }
}

bool deleteFile(Map *user , char file_name[] , AFD *afd)
{
  File *file = NULL , *prefile = NULL , *temp ;
  file = afd->head ;
  //在打开文件中查找
  while(file)
  {
      if(strcmp(file_name , file->file_name) == 0)
      {
          cout<<"\""<<file_name<<"\" is open , please close it before ! \n" ;
          return false ;
      }
      file = file->next ;
  }
  file = user->file ;
  //在文件中进行查找
  while(file)
  {
    if(strcmp(file_name , file->file_name) == 0)
    {
       if(file == user->file)
       {
           temp = file ;
           user->file = file->next ;
       }
       else
       {
           temp = file ;
           prefile->next = file->next ;
       }
       delete temp ;
       return true ;
    }
    prefile = file ;
    file = file->next ;
  }
  if(prefile->next == NULL)
  {
      cout<<"user "<<user->userName<<" has not the file \""<<file_name<<"\""<<endl;
  }
  return false ;
}

bool openFile(Map *user , char file_name[] , AFD *afd , bool open_file_protect[])
{
   File *file = NULL ;
   file = user->file ;
   while(file)
   {
       if(strcmp(file->file_name , file_name) == 0)
       {
           break ;
       }
       file = file->next ;
   }
   if(file)
   {
       File *xfile ;
       xfile = (File*)malloc(sizeof(File)) ;
       if(xfile == NULL)
       {
           return false ;
       }
       *xfile = *file ;
       //根据文件的权限进行打开权限的赋值
       if(xfile->file_protect[0] >= open_file_protect[0])
       {
           xfile->open_file_protect[0] = open_file_protect[0] ;
       }
       else
       {
            cout<<"no read priority ! "<<endl;
            return false ;
       }
       if(xfile->file_protect[1] >= open_file_protect[1])
       {
           xfile->open_file_protect[1] = open_file_protect[1] ;
       }
       else
       {
            cout<<"no write priority ! "<<endl;
            return false ;
       }
        if(xfile->file_protect[2] >= open_file_protect[2])
       {
           xfile->open_file_protect[2] = open_file_protect[2] ;
       }
       else
       {
            cout<<"no excute priority ! "<<endl;
            return false ;
       }
       xfile->next = NULL ;
       if(afd->head == NULL)
       {
           afd->head = afd->tail = xfile ;
           afd->current_open += 1 ;
       }
       else if(afd->current_open < afd->max_open)
       {
           afd->tail->next = xfile ;
           afd->tail = xfile ;
           afd->current_open += 1 ;
       }
       else
       {
           cout<<"The open file is too many ! " <<endl ;
           return false ;
       }
   }
   else
   {
       cout<<"the "<<file_name<<" is not exit !"<<endl ;
       return false ;
   }
}

bool closeFile(AFD *afd , char file_name[])
{
    File *file = NULL  , *preFile = NULL  , *temp = NULL ;
    //在打开文件链表中进行查找
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file == afd->head)
            {
                if(file == afd->tail)
                {
                    temp = file ;
                    afd->head = afd->tail = NULL ;
                }
                else
                {
                    temp = file ;
                    afd->head = file->next ;
                }
            }
            else if(file == afd->tail)
            {
                temp = file ;
                preFile->next = NULL ;
                afd->tail = preFile ;
            }
            else
            {
                temp =file ;
                preFile->next = file->next ;
            }
            delete temp ;
            return true ;
        }
        preFile = file ;
        file = file->next ;
    }
    cout<<"The file is not exit ! "<<endl ;
    return false ;
}

bool readFile(AFD *afd , char file_name[])
{
    File *file = NULL ;
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file->open_file_protect[0])
            {
                file->read++ ;
                return true ;
            }
            else
            {
                cout<<"no read priority ! \n"<<endl ;
                return false ;
            }
        }
        file = file->next ;
    }
  cout<<"no such file ! "<<endl ;
  return false ;
}

bool writeFile(AFD *afd , char file_name[])
{
     File *file = NULL ;
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file->open_file_protect[1])
            {
                file->write++ ;
                return true ;
            }
            else
            {
                cout<<"no write priority ! \n"<<endl ;
                return false ;
            }
        }
        file = file->next ;
    }
  cout<<"no such file ! "<<endl ;
  return false ;
}

void displayOpenFile(AFD *afd , Map *user)
{
    cout<<"The open file of "<<user->userName<<" : "<<endl ;
    File *file ;
    file = afd->head ;
    while(file)
    {
        cout<<file->file_name<<" "<<file->file_protect[0]<<" "<<file->file_protect[1]<<" "<<file->file_protect[2]<<" "<<file->file_length<<" " ;
        cout<<"readcout : "<<file->read<<" writecout : "<<file->write<<endl ;
        file = file->next ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542

四、测试结果

这里写图片描述

这里写图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/263722
推荐阅读
相关标签
  

闽ICP备14008679号