红联Linux门户
Linux帮助

Linux下清空多级目录(C++)

发布时间:2007-06-08 16:51:26来源:红联作者:Skill
#include
#include
#include
#include

#define _XOPEN_SOURCE 500

int deal( const char *file ,const struct stat* sb, int flags, struct FTW *f);

int main(){
using namespace std;
int flags = FTW_DEPTH | FTW_MOUNT | FTW_PHYS;
// FTW_DEPTH: 深度优先遍例
// FTW_MOUNT: 防止遍历过程中穿越文件系统
// FTW_PHYS: 不跟随符号链接的路径遍例
int rc;
rc = nftw( "./a",rmdirHelper,100,flags );
if( rc == 0 )
cout << "successed" << endl;
else
cout << "failed" << endl;
return 0;
}

int rmdirHelper( const char *file, const struct stat* sb, int flags, struct FTW *f ){
using namespace std;
if( f->level < 0 ) return 0;
if( f->level > INT_MAX ) return 0;
remove( file );
if( flags == FTW_DNR ){// add code
cout << "find: " << file << "permission denied "< return -1;
}else if( flags == FTW_NS ){
cout << "find: stat() failed " << endl;
return -1;
}else{
cout << "delete " << file << endl;
return 0;
}
}
文章评论

共有 0 条评论