1、先完成找文件的函数:void GetFileInDir(string dirName){DIR *Dir=NULL;struct dirent *file=NULL;if(dirName[dirName.size()-1]!='/'){dirName+="/";}if((Dir=opendir(dirName.c_str()))==NULL){cerr<<"Can't open Directory"<<endl;exit(1);}while(file=readdir(Dir)){//if the file is a normal fileif(file->d_type==DT_REG){cout<<dirName+file->d_name<<endl;}//if the file is a directoryelse if(file->d_type==DT_DIR&&strcmp(file->d_name,".")!=0&&strcmp(file->d_name,"..")!=0){GetFileInDir(dirName+file->d_name);}}}
2、加入main函数相应的main 函数:int main(int argc,char*argv[]){if(argc<2){cerr<<"NeedDirectory"<<endl;exit(1);}string dir=argv[1];GetFileInDir(dir);}
3、随后加入要找的文件,从main 函数中穿参数到函数GetFileInDirGetFileInDir(dir, filename)然后再GetFileInDir 函数中多加一个文件是否找到的判断GetFileInDir