阅读内容

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

[日期:2007-06-09] 来源:Linux公社  作者:Linux


#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;
}
}



相关资讯      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:

L
I
N
U
X


GOOGLE搜索
Google