c++二级文件系统文档

来源:互联网 发布:淘宝手表代购是真的吗 编辑:程序博客网 时间:2024/05/17 14:15

#include "stdio.h"

#include <iostream>  

#include "string.h"

#include "iomanip"

 

#define FILENAME_LENGTH 10 //文件名称长度

#define COMMAND_LENGTH 10  //命令行长度

#define PARA_LENGTH 30    //参数长度

using namespace std;

//账号结构

typedef struct users

{

char     name[8];

char     pwd[10];

}users;

//文件结构

 

struct fnode

{

char filename[FILENAME_LENGTH];//文件名称

int  isdir;//是不是目录

int isopen;//打开没

char content[255];//长度内容

fnode *parent;//父节点

fnode *child;//子节点

fnode *prev;//先驱节点

fnode *next;//后继节点

};

//账号

users usrarray[8] =

{

"admin","admin",

};

fnode *initfile(char filename[],int isdir);

void createroot();

int run();

int findpara(char *topara);

bool chklogin(char *users, char *pwd);

void help();

int mkdir();

int create();

int read();

int write();

int del();

int cd();

int dir();

fnode *root,*recent,*temp,*ttemp;//根节点,最近节点,临时节点,临时节点

char para[PARA_LENGTH],command[COMMAND_LENGTH],temppara[PARA_LENGTH],recentpara[PARA_LENGTH];//para 文目录总长度,字符串类型

//当前路径              命令                    节点路径             最后找的文件夹

//创建文件与目录结点

fnode* initfile(char filename[],int isdir)

{

fnode *node=new fnode;

strcpy(node->filename,filename);//字符串拷贝

node->isdir=isdir;//创建是不是目录的标识

node->isopen=0;

node->parent=NULL;

node->child=NULL;

node->prev=NULL;

node->next=NULL;

return node;

}

//创建文件存储结点,相当于/就是根目录

void createroot ()

{

recent=root=initfile("/",1);

root->parent=NULL;

root->child=NULL;

root->prev=root->next=NULL;

strcpy(para,"/");//

 

}

int mkdir()//建立目录

{

temp=initfile(" ",1);//建立个临时节点

cout<<"请输入目录名"<<endl;

cin>>temp->filename;

if(recent->child==NULL)//当前节点无子节点,添加改节点为当前节点的子节点

{

temp->parent=recent;

temp->child=NULL;

recent->child=temp;

temp->prev=temp->next=NULL;

cout<<"目录创建成功"<<endl;

return 1;

 

}

else if(recent->child!=NULL)//存在父亲节点,同时存在子字节,遍历查找该节点的最后一个子节点,最后一个添加本节点,是最后一个子节点添加,不是父节点

{

ttemp=recent->child;//获得首节点

while(ttemp)

{

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)//查找过程中找到该文件则显示已经存在

{

cout<<"目录已存在"<<endl;

return 1;

}

    ttemp=ttemp->next;

}

}

if(!ttemp) {

 ttemp=recent->child;//获得首节点

while(ttemp->next)

{

    ttemp=ttemp->next;

}

ttemp->next=temp;

temp->parent=NULL;

temp->child=NULL;

temp->prev=ttemp;

temp->next=NULL;

cout<<"目录创建成功"<<endl;

}

return 1;//true

}

int create()

{

temp=initfile(" ",0);//创建新的节点

cout<<"请输入文件名和扩展名"<<endl;

cin>>temp->filename;//文件名

cout<<"请输入文件内容"<<endl;

cin>>temp->content;//文件内容

if(recent->child==NULL)//本节点无子节点,新建节点插入到该节点

{

temp->parent=recent;

temp->child=NULL;

recent->child=temp;

temp->prev=temp->next=NULL;

cout<<"文件建立成功!"<<endl;

return 1;

}

    if(recent->child!=NULL)

{

//切换到子节点的最后一个节点,同时验证添加

ttemp=recent->child;

while(ttemp)

{

if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0)

{

printf("对不起,文件已存在!");

cout<<endl;

return 1;

}

ttemp=ttemp->next;

}

}

if(!ttemp) {

 ttemp=recent->child;//获得首节点

while(ttemp->next)

{

    ttemp=ttemp->next;

}

ttemp->next=temp;

temp->parent=NULL;

temp->child=NULL;

temp->prev=ttemp;

temp->next=NULL;

cout<<"文件建立成功!"<<endl;

}

return 1;

}

int dir()

{

int i=0,j=0;

temp=new fnode;

temp=recent;

if(temp!=root)

{cout<<"\ <DIR> ";cout<<".."<<endl;i++;}

if(temp->child==NULL)

{

cout<<"Total: "<<" 目录数" <<i<<" 文件数"<< j <<endl;

return 1;

}

temp=temp->child;

while(temp)

{

if(temp->isdir)

{cout<<"<DIR>\ ";cout<<temp->filename<<endl;i++;}

else

{cout<<"<FILE> ";cout<<temp->filename<<endl;j++;}

temp=temp->next;

}

cout<<"Total: "<<" 目录数" <<i<<"文件数"<< j <<endl;

return 1;

}

 

int read()

{

char filename[FILENAME_LENGTH];

cout<<"请输入文件名"<<endl;

cin>>filename;

if(recent->child==NULL)

{

cout<<"文件不存在!"<<endl;

return 1;

}

if(strcmp(recent->child->filename,filename)==0)

{

cout<<recent->child->content<<endl;

return 1;

}

else

{

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{cout<<temp->next->content<<endl;

return 1;}

}

cout<<"文件不存在!"<<endl;

 

}

return 1;

}

int write()

{

char filename[FILENAME_LENGTH];

    cout<<"请输入文件名"<<endl;

cin>>filename;

if(recent->child==NULL)

{

cout<<"文件不存在!"<<endl;

return 1;

}

if(strcmp(recent->child->filename,filename)==0)

{

recent->child->isopen=1;//设置文件标记为打开

cout<<"请输入内容"<<endl;

cin>>recent->child->content;

recent->child->isopen=0;//设置文件标记为关闭

cout<<"文件写入成功!"<<endl;

return 1;

}

else

{

temp=recent->child;

while(temp->next)

{

if(strcmp(temp->next->filename,filename)==0)

{

recent->child->isopen=1;//设置文件标记为打开

cout<<"请输入内容"<<endl;

cin>>temp->next->content;

recent->child->isopen=0;//设置文件标记为关闭

cout<<"文件写入成功!"<<endl;

return 1;}

}

cout<<"文件不存在!"<<endl;

}

return 1;

}

int cd()

{  

char topara[PARA_LENGTH];

    cout<<"请输入命令"<<endl;

cin>>topara;

if(strcmp(topara,"..")==0)//输入..,返回上级目录

{

int i;

while(recent->prev)//向前驱返回,前驱就没当前节点

recent=recent->prev;//通过前驱找到父节点

if(recent->parent)//通过

{

recent=recent->parent;

}

i=strlen(para);//字符串截取到父节点那,显示父节点目录

while(para[i]!='/' && i>0) i--;

if(i!=0)

para[i]='\0';//走到上一级目录不是根目录para[i]='\0'

else

para[i+1]='\0';//根目录

}

else

{

findpara(topara);

}

return 1;//true

}

int findpara(char *topara)

{

int i=0;

int sign=1;

if(strcmp(topara,"/")==0)//返回根节点

{

recent=root;

strcpy(para,"/");

return 1;

}

temp=recent;//当前节点

strcpy(temppara,para);//temppara当前节点路径

if(topara[0]=='/')

{

recent=root->child;

i++;

strcpy(para,"/");

}

else

{

if(recent!=NULL && recent!=root)

strcat(para,"/");

if(recent && recent->child)

{

if(recent->isdir)

recent=recent->child;

else

{

printf("路径错误!\n");

return 1;

}

}

}

while(i<=strlen(topara) && recent)//最后截取到最底层目录

{

int j=0;

if(topara[i]=='/' && recent->child)

{

i++;

if(recent->isdir)

recent=recent->child;

else

{printf("路径错误\n");

return 0;

}

strcat(para,"/");

}

while(topara[i]!='/' && i<=strlen(topara))

{

recentpara[j]=topara[i];

i++;j++;

}

recentpara[j]='\0';

while((strcmp(recent->filename,recentpara)!=0 || (recent->isdir!=1)) && recent->next!=NULL)

{

recent=recent->next;

}

if(strcmp(recent->filename,recentpara)==0)

{

if(recent->isdir==0)

{strcpy(para,temppara);//增加路径地址

recent=temp;

printf("是文件不是目录。\n");

return 0;

}

strcat(para,recent->filename);

}

if(strcmp(recent->filename,recentpara)!=0 || recent==NULL)

{

strcpy(para,temppara);

recent=temp;

printf("输入路径错误\n");

return 0;

}

}

return 1;

}

int del()

{

char filename[FILENAME_LENGTH];

cout<<"请输入文件名"<<endl;

cin>>filename;

temp=new fnode;

if(recent->child)

{

temp=recent->child;

while(temp->next && (strcmp(temp->filename,filename)!=0 || temp->isdir!=0))

temp=temp->next;

if(strcmp(temp->filename,filename)!=0)

{

cout<<"不存在该文件!"<<endl;

return 0;

}

}

else//相当于temp为空

{

cout<<"不存在该文件!"<<endl;

return 0;

}

if(temp->parent==NULL)

{

temp->prev->next=temp->next;

if(temp->next)

temp->next->prev=temp->prev;

temp->prev=temp->next=NULL;

}

else

{

if(temp->next)

temp->next->parent=temp->parent;

temp->parent->child=temp->next;

}

delete temp;

cout<<"文件已删除!"<<endl;

return 1;

}

bool chklogin(char *users, char *pwd)

{

int i;

for(i=0; i<8; i++)

{

if( (strcmp(users,usrarray[i].name)==0) && (strcmp(pwd,usrarray[i].pwd)==0))

return true;

}

return false;

}

void menu(void)

{

cout<<"                  命 令 一 览              "<<endl;

cout<<endl;

cout<<"create:             建立文件。               "<<endl;

cout<<"read:               读取文件。                 "<<endl;

cout<<"write:              写入文件,支持多线程         "<<endl;

cout<<"del   :             删除文件。                 "<<endl;

cout<<"mkdir:              建立目录。               "<<endl;

cout<<"dir:                显示目录下的文件。        "<<endl;

cout<<"cd:                 切换目录。                 "<<endl;

cout<<"logout:             退出登录。               "<<endl;

}

int run()

{

//int i=strlen(para);

//cout<<i<<endl;

cout<<"linux:"<<para<<">";//显示目录位置

cin>>command;

if(strcmp(command,"mkdir")==0)

{mkdir();return 1;}

else if(strcmp(command,"dir")==0)

{dir();return 1;}

else if(strcmp(command,"cd")==0)

{cd();return 1;}

else if(strcmp(command,"create")==0)

{create();return 1;}

else if(strcmp(command,"read")==0)

{read();return 1;}

else if(strcmp(command,"write")==0)

{write();return 1;}

else if(strcmp(command,"del")==0)

{del();return 1;}

else if(strcmp(command,"help")==0)

{menu();return 1;}

else if(strcmp(command,"logout")==0)

{return 0;}

else{

return 1;

}

}

void main()

{

int i=0;

bool in=false;

char users[8],pwd[12];

cout<<"|-----------------------------------------------------------------|"<<endl;

cout<<"|                          模拟 文件 系统                      |"<<endl;

cout<<"|                     账号:admin      密码:admin                |"<<endl;

cout<<"|                        你只有三次机会来试验账号                |"<<endl;

cout<<"|_________________________________________________________________|"<<endl;

cout<<endl;

while(i<3)

{

cout<<"Login:";

cin>>users;

cout<<"Pass:";

cin>>pwd;

if(chklogin(users,pwd))

{in=true;

 break;

}

i++;

}

menu();

createroot();

while(in)

{

if(!run())

break;

}

}

0 0