【发布时间】:2018-09-29 20:29:25
【问题描述】:
您好,我必须在 c (linux) 函数中编写以从目录中删除所有文件,我不能使用删除函数或 execl 只能取消链接和 rmdir。
我使用了 readdir:
int removefile( char *file)
{
struct dirent * entry;
DIR *dir;
char *p;
char *d;
char *tmp;
dir = opendir(file);
errno =0;
strcpy( d, file );//kopiowanie file do p
strcat( d, "/" );//dodawanie /
strcpy(tmp,d);//kopiowanie d do tmp
strcpy(p,d); //kopiowanie d do p
while((entry=readdir(dir)) !=NULL)
{
if(entry->d_type==DT_REG)
{
strcat(d,entry->d_name);
int a=unlink(d);
strcpy(d,tmp);
}
else if(entry->d_type==DT_DIR)
{
strcat(p,entry->d_name);
int b=removefile(p);
int c=rmdir(p);
strcpy(p,tmp);
}
}
closedir(dir);
return 0;
}
但我得到内存访问冲突 谢谢
【问题讨论】:
-
你必须检查
opendir()返回非NULL指针。 -
您有 多个 错误,其中大多数(如果不是全部)与指针有关。例如,
d或p指向哪里?opendir成功了吗?