c/c++常用代码--清空目录

#pragma once

#include <io.h>
#include <stdio.h>
#include <string>
#include <direct.h>


static void clean_dir(const char* szDir)
{
    if (szDir == NULL || strlen(szDir) == 0)
        return;

    std::string strDir = szDir;

    char c = strDir[strDir.length() - 1];
    if (c != '/' && c != '\')
        strDir += '/';

    struct _finddata_t fd;
    long h=_findfirst((strDir + "*.*").c_str(), &fd);
    if (h == -1)
    {
        return ;
    }

    do
    {
        if (strcmp(fd.name, ".") == 0
            || strcmp(fd.name, "..") == 0)
            continue;

        if (fd.attrib & (_A_SYSTEM | _A_HIDDEN))
            continue;

        if (fd.attrib & _A_SUBDIR)
        {
            std::string str = strDir + fd.name + "/";
            clean_dir(str.c_str());
            rmdir(str.c_str());
            continue;
        }
        
        std::string str = strDir + fd.name;
        printf("%s ", str.c_str());        

        remove(str.c_str());
    }
    while (_findnext(h, &fd)==0);

    _findclose(h);
}

原文地址:https://www.cnblogs.com/vc60er/p/3929934.html