c++ 删除文件夹和所有文件/子目录

如何在C中删除所有文件/子目录(递归删除)的文件夹?
认真:

system( "rm -rf /path/to/directory" )

也许更多的是你正在寻找,但unix具体:

/* Implement system( "rm -rf" ) */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/syslimits.h>
#include <ftw.h>


/* Call unlink or rmdir on the path, as appropriate. */
int
rm( const char *path, const struct stat *s, int flag, struct FTW *f )
{
    int status;
    int (*rm_func)( const char * );

    switch( flag ) {
    default:     rm_func = unlink; break;
    case FTW_DP: rm_func = rmdir;
    }
    if( status = rm_func( path ), status != 0 )
        perror( path );
    else
        puts( path );
    return status;
}


int
main( int argc, char **argv )
{
    while( *++argv ) {
        if( nftw( *argv, rm, OPEN_MAX, FTW_DEPTH )) {
            perror( *argv );
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}
翻译自:https://stackoverflow.com/questions/1149764/delete-folder-and-all-files-subdirectories

转载注明原文:c++ 删除文件夹和所有文件/子目录