linux实现简单命令解释器,调用opendir(),readdir(),closedir函数报错!

qq_37797800 2018-12-09 02:58:10
源文件:
#include<iostream>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<fcntl.h>
#include<time.h>
#include<queue>
#include<ftw.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>

using namespace std;

void mypwd(); //Display the path name of the current directory
void mydir(); //List all directories and files in the specified directory name
void mycd(); //Change the current working directory
void mymkdir(); // new directory
void myrmdir(); //delete directory
void myrename(); //Rename a file or directory
void mycopy();//Copy an existing file
void myfind(); /*Find the specified file in the specified directory and its subdirectories, and output the absolute path of the found file*/
int fn(const char *file, const struct stat *sb, int flag);

int main(int argc, char *argv[])
{
cout<<"********************************************************"<<endl;
cout<<"Simulate Simple Command Interpreter in Linux Environment"<<endl;
cout<<"*****Software class one;yu hua wen ;Student ID:26*******"<<endl;
cout<<"****command***:"<<endl;
cout<<"1.mypwd "<<endl;
cout<<"2.mydir <dirname>"<<endl;
cout<<"3.mycd <dirname or path> "<<endl;
cout<<"4.mymkdir <dirname> "<<endl;
cout<<"5.myrmdir <dirname> "<<endl;
cout<<"6.myexit "<<endl;
cout<<"7.myrename <old filename> <new filename> "<<endl;
cout<<"8.mycopy <old filename> <new filename or path> "<<endl;
cout<<"9.myfind <dirname>"<<endl;
cout<<"********************************************************"<<endl;
cout<<"********************************************************"<<endl;

string str;
while(str != "exit") {
cout<<"[yuhuawen@]$ ";
cin>>str;
if(str == "mypwd"){
mypwd();
}
else if(str == "mydir") {
mydir();
}
else if(str == "mycd") {
mycd();
}
else if(str == "mymkdir"){
mymkdir();
}
else if(str == "myrmdir"){
myrmdir();
}
else if(str == "myexit") {//Exit command interpreter
break;
}
else if(str == "myrename") {
myrename();
}
else if(str == "mycopy") {
mycopy();
}
else if(str == "myfind") {
myfind();
}
else cout<<str<<" commond not found!"<<endl;
}
return 0;
}

void mypwd()
{
char br[80];
getcwd(br,sizeof(br));
cout<<br<<endl;
}

void mydir()
{
DIR * dir;
struct dirent* ptr;
char *dirname;
cin>>dirname;
dir = opendir(dirname); /*Used to open the directory specified by parameter dirname and return the directory stream in the form of DIR* */
if(dir == NULL)
{
cout<<"cannot open directory"<<endl;
}
while((ptr = readdir(dir)) != NULL)
{ //encounter .and.. skip
if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0){}
else
cout<<ptr->d_name<<" ";
}
closedir(dir); //Close the directory stream referred to by the parameter dir.
cout<<endl;
}
void mycd()
{
char dirname[20];
cin>>dirname;
if(chdir(dirname) == -1)
{
cout<<"the directory is not exit!!!"<<endl;
}
else
{
cout<<"change directory success!!!"<<endl;
}
}

void mymkdir()
{
char filename[20];
cin >> filename;
if(mkdir(filename, 0777) == 0) //Give maximum permission to create directories
{
cout<<filename<<" Create directory successfully!"<<endl;
}
else
{ cout<<filename<<" Failed to create directory!"<<endl;
}
}
void myrmdir()
{
char filename[20];
cin >> filename;
if(remove(filename) == 0)
{
cout<<filename<<" delete successful!!!"<<endl;
}
else
cout<<filename<<" delete failure!!!"<<endl;
}
void myrename()
{
char oldname[20],newname[20];
cin>>oldname>>newname;
if(rename(oldname,newname) == 0)
{
cout<<oldname<< " success change to "<<newname<<endl;
}
else
cout<<"change failure"<<endl;
}
void mycopy()
{
char firstpath[255]; //the source path
char secondpath[255]; //The destination path
char param_list[3][256];//save something cin
cin>>param_list[1]>>param_list[2];
FILE *newfp;
FILE *oldfp;
char ch;
getcwd(firstpath,255);// copy this directory's path to firstpath,firstpath's size is 255
getcwd(secondpath,255);
strcat(firstpath,"/");
strcat(firstpath,param_list[1]);
strcat(secondpath,"/");
strcat(secondpath,param_list[2]);
if((oldfp=fopen(firstpath,"r"))==NULL)
{
cout<<"can't open the file"<<endl;
}
if((newfp=fopen(secondpath,"w"))==NULL)
{
cout<<"fail to new a file!"<<endl;
}
while((ch=fgetc(oldfp))!=EOF)
{ fputc(ch,newfp); }
fclose(oldfp);
fclose(newfp);
cout<<"copy successful!!!"<<endl;
}
void myfind()
{
char dirname[50];
cin>>dirname;
ftw(dirname, fn,500);
}
int fn(const char *file, const struct stat *sb, int flag)
{
if(flag == FTW_D) //directory
cout << file <<"-- directory"<<endl;
else if(flag == FTW_F) //file
cout << file <<"-- file"<<endl;
return 0;
}


运行成功了,但是调用下面这个函数会出现core文件,报错Segmentation fault (core dumped)!!!!什么问题
本人第一次玩linux,轻喷
void mydir()
{
DIR * dir;
struct dirent* ptr;
char *dirname;
cin>>dirname;
dir = opendir(dirname); /*Used to open the directory specified by parameter dirname and return the directory stream in the form of DIR* */
if(dir == NULL)
{
cout<<"cannot open directory"<<endl;
}
while((ptr = readdir(dir)) != NULL)
{ //encounter .and.. skip
if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0){}
else
cout<<ptr->d_name<<" ";
}
closedir(dir); //Close the directory stream referred to by the parameter dir.
cout<<endl;
}
...全文
207 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
自己gdb调试一下看看具体哪里报错的
qq_37797800 2018-12-09
  • 打赏
  • 举报
回复
有人懂吗,可以加分

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧