linux c语言清空目录

#include <dirent.h>

int EmptyDir(char *destDir)
{
	DIR *dp;
	struct dirent *entry;
	struct stat statbuf;
	if ((dp = opendir(destDir)) == NULL)
	{
		mkdir(destDir, 0777);
		if ((dp = opendir(destDir)) == NULL)
		{
			printf("opendir error\n");
			return -1;
		}
	}
	if(0 != chdir(destDir))
    {
        printf("chdir error\n");
        return -1;
    }

	while ((entry = readdir(dp)) != NULL)
	{
		lstat(entry->d_name, &statbuf);
		if (S_ISREG(statbuf.st_mode))
		{
			remove(entry->d_name);
		}
	}
 
	return 1;
}

参考:https://blog.csdn.net/Blaider/article/details/41080581

猜你喜欢

转载自blog.csdn.net/eidolon_foot/article/details/111387652