实现Linux下清空多级目录(C++)
#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;
}
}
#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;
}
}
相关推荐
farwang 2020-11-25
星愿心愿 2020-11-24
tianhuak 2020-11-24
zhjn0 2020-11-24
昭君出塞 2020-11-23
bluecarrot 2020-11-23
linuxwcj 2020-10-21
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
waterhorse 2020-09-19
MRFENGG 2020-11-11
rainandtear 2020-10-30
kyssfanhui 2020-10-20
liuhangtiant 2020-10-20