c c++删除目录rm函数代码求解

操作系统,其中一步“删除目录” “rm函数”需要代码改一下,我在网上实在找不到解决办法了,恳请各位帮我看看,谢谢!
我想要达到的结果:输mkdir创建几个目录后,再输rm可达到删除指定目录的操作
恳请各位帮帮我!

#include "stdio.h"
#include
#include "string.h"

#define FILENAME_LENGTH 10 //文件名称长度
#define COMMAND_LENGTH 10 //命令行长度
#define PATH_LENGTH 30 //参数长度
using namespace std;

struct filenode /初始化/
{
char filename[FILENAME_LENGTH];
int isdir;
char content[255];
filenode *parent;
filenode *child;
filenode *prev;
filenode *next;
};

filenode *initnode(char filename[],int isdir);
void createroot();
int run();
int findpath(char *topath);
void help();
int mkdir();
int create();
int read();
int write();
int del();
int rm();
int cd();
int dir();
filenode *root,*recent,*temp,*ttemp,*temp_child;
char path[PATH_LENGTH],command[COMMAND_LENGTH],temppath[PATH_LENGTH],recentpath[PATH_LENGTH];

//创建文件或目录的存储节点
filenode* initnode(char filename[],int isdir)
{
filenode *node=new filenode;
strcpy(node->filename,filename);
node->isdir=isdir;
node->parent=NULL;
node->child=NULL;
node->prev=NULL;
node->next=NULL;
return node;
}

//初始化文件系统根结点
void createroot () /创建文件 create/
{
recent=root=initnode("/",1);
root->parent=NULL;
root->child=NULL;
root->prev=root->next=NULL;
strcpy(path,"/");
}

int read() /读文件 read/
{
char filename[FILENAME_LENGTH];
cin>>filename;
if(recent->child==NULL)
{
printf("文件不存在!\n");
return 1;
}
if(strcmp(recent->child->filename,filename)==0)
{
cout<child->content<<endl;
return 0;
}
else
{
temp=recent->child;
while(temp->next)
{
if(strcmp(temp->next->filename,filename)==0)
{
cout<next->content<<endl;
return 0;
}
}
printf("文件不存在!\n");
return 1;
}
}

int write() /写文件 write/
{
char filename[FILENAME_LENGTH];
cin>>filename;
if(recent->child==NULL)
{
printf("文件不存在!\n");
return 1;
}
if(strcmp(recent->child->filename,filename)==0)
{
cin>>recent->child->content;
printf("文件写入成功!\n");
return 0;
}
else
{
temp=recent->child;
while(temp->next)
{
if(strcmp(temp->next->filename,filename)==0)
{
cin>>temp->next->content;
printf("文件写入成功!\n");
return 0;
}
}
printf("文件不存在!\n");
return 1;
}
}

int del() /删除文件 delete/
{
char filename[FILENAME_LENGTH];
cin>>filename;
temp=new filenode;

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)
{
printf("不存在该文件!\n");
return 0;
}
}
else
{
printf("不存在该文件!\n");
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;
printf("文件已删除!\n");
return 0;
}

int mkdir() /创建目录 mkdir/
{
temp=initnode(" ",1);
cin>>temp->filename;
if(recent->child==NULL)
{
temp->parent=recent;
temp->child=NULL;
recent->child=temp;
temp->prev=temp->next=NULL;
}
else
{
ttemp=recent->child;
while(ttemp->next)
{
ttemp=ttemp->next;
if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==1)
{
printf("创建目录成功!");
return 1;
}
}
ttemp->next=temp;
temp->parent=NULL;
temp->child=NULL;
temp->prev=ttemp;
temp->next=NULL;
}
return 0;
}

int create()
{
temp=initnode(" ",0);
cin>>temp->filename;
cin>>temp->content;
if(recent->child==NULL)
{
temp->parent=recent;
temp->child=NULL;
recent->child=temp;
temp->prev=temp->next=NULL;
printf("恭喜你文件创建成功!\n");
}
else
{
ttemp=recent->child;
while(ttemp->next)
{
ttemp=ttemp->next;
if(strcmp(ttemp->filename,temp->filename)==0&&ttemp->isdir==0)
{
printf("文件名重复!");
return 1;
}
}
ttemp->next=temp;
temp->parent=NULL;
temp->child=NULL;
temp->prev=ttemp;
temp->next=NULL;
printf("恭喜你文件创建成功!\n");
}

return 0;

}

int cd() /切换目录 cd/
{ char topath[PATH_LENGTH];
cin>>topath;
if(strcmp(topath,".")==0)
return 0;
if(strcmp(topath,"..")==0)
{
int i;
while(recent->prev)
recent=recent->prev; //向前回溯,找到第一次创建的目录
if(recent->parent)
{
recent=recent->parent;
}

 i=strlen(path);
// printf("%d %s\n",i,path);
 while(path[i]!='/' && i>0) 
     i--; //找到最右边的/
    if(i!=0)
    {     path[i]='\0';    
         //printf("%s",path); //path中不止有一个/
    }

    else 
         path[i+1]='\0';
 }
else
{
    findpath(topath); 
}
return 0;

}

int findpath(char *topath)
{
unsigned int i=0;
int sign=1;
if(strcmp(topath,"/")==0) //如果命令是cd /
{
recent=root;
strcpy(path,"/");
return 0;
}
temp=recent;
strcpy(temppath,path);
if(topath[0]=='/') //cd命令以cd /开始
{
recent=root->child;
i++;
strcpy(path,"/");
// printf("\n%s",path);
}
else
{
if(recent!=NULL && recent!=root)
{
strcat(path,"/");
// printf("\n%s\n",path);
}

    if(recent && recent->child)
    {
        if(recent->isdir)
             recent=recent->child;
        else
        {
            printf("路径错误!\n");
            return 1;
         }
    }

}
while(i<=strlen(topath) && recent)
{
int j=0;
if(topath[i]=='/' && recent->child)
{
i++;
if(recent->isdir)
recent=recent->child;
else
{printf("路径错误\n");
return 1;
}
strcat(path,"/");
}
while(topath[i]!='/' && i<=strlen(topath))
{
recentpath[j]=topath[i];
i++;j++;
}
recentpath[j]='\0';
while((strcmp(recent->filename,recentpath)!=0 || (recent->isdir!=1)) && recent->next!=NULL)
{
recent=recent->next;
}
if(strcmp(recent->filename,recentpath)==0)
{
if(recent->isdir==0)
{strcpy(path,temppath);
recent=temp;
printf("是文件不是目录。\n");
return 1;
}
strcat(path,recent->filename);
}
if(strcmp(recent->filename,recentpath)!=0 || recent==NULL)
{
strcpy(path,temppath);
recent=temp;
printf("输入路径错误\n");
return 1;
}
}
return 0;
}

int dir() /展示目录 dir/
{
int i=0,j=0;
temp=new filenode;
temp=recent;
if(temp!=root)
{cout<<"

"<<".."<<endl;i++;}
if(temp->child==NULL)
{
cout<<"Total: "<<" directors " <<i<<" files "<< j <<endl;
return 1;
}
temp=temp->child;
while(temp)
{
if(temp->isdir)
{cout<<""<filename<<endl;i++;}
else
{cout<<" "<filename<<endl;j++;}
temp=temp->next;
}
cout<<"Total: "<<" directors " <<i<<" files "<< j <<endl;
}

int rm() /删除目录 rm/
{
char filename[FILENAME_LENGTH];
cin>>filename;
temp=new filenode;

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
{
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;
printf("目录已删除!\n");
return 0;
}

void help() /综合调用/
{
cout<<endl;
cout<<"create: 建立文件。 "<<endl;
cout<<"read: 读取文件。 "<<endl;
cout<<"write: 写入文件。 "<<endl;
cout<<"delete: 删除文件。 "<<endl;
cout<<"rm: 删除目录。 "<<endl;
cout<<"mkdir: 建立目录。 "<<endl;
cout<<"cd: 切换目录。 "<<endl;
cout<<"dir: 显示目录。 "<<endl;
cout<<"logout: 退出登录。 "<<endl;
}

int run()
{
cout<<"filesystem:"<<path<<">";
cin>>command;
if(strcmp(command,"mkdir")==0)
mkdir();
else if(strcmp(command,"dir")==0)
dir();
else if(strcmp(command,"cd")==0)
cd();
else if(strcmp(command,"create")==0)
create();
else if(strcmp(command,"read")==0)
read();
else if(strcmp(command,"write")==0)
write();
else if(strcmp(command,"delete")==0)
del();
else if(strcmp(command,"help")==0)
help();
else if(strcmp(command,"logout")==0)
return 0;
else
cout<<"请参考help提供的命令列表!"<<endl;
return 1;
}

int main()
{
cout<<""<<endl;
cout<<"操作系统课程项目<<endl;
cout<<"
简单文件系统模拟 "<<endl;
cout<<"
键入help可以获取帮助 "<<endl;
cout<<"
"<<endl;
cout<<"*"<<endl;
cout<<endl;
createroot();
while(1)
{
if(!run())
break;
}
}

我想要达到的结果:输mkdir创建几个目录后,再输rm可达到删除指定目录的操作
恳请各位帮帮我!