当前位置: 技术问答>linux和unix
如何删除指定文件夹下所有内容?
来源: 互联网 发布时间:2015-11-07
本文导语: 如何删除指定文件夹下的所有子文件夹和文件。用c语言 | /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by...
如何删除指定文件夹下的所有子文件夹和文件。用c语言
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char buf[128];
buf[0] = 0x0;
if (argc != 2){
printf("usage : %s n");
exit(EXIT_FAILURE);
}
if(argv[1][strlen(argv[1])-1] == '/')
sprintf(buf, "rm -fr %s*", argv[1]);
else
sprintf(buf, "rm -fr %s/*", argv[1]);
int n = system(buf);
if (n == -1){
printf("delete failed!");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include
#include
#include
#include
int main(int argc, char *argv[])
{
char buf[128];
buf[0] = 0x0;
if (argc != 2){
printf("usage : %s n");
exit(EXIT_FAILURE);
}
if(argv[1][strlen(argv[1])-1] == '/')
sprintf(buf, "rm -fr %s*", argv[1]);
else
sprintf(buf, "rm -fr %s/*", argv[1]);
int n = system(buf);
if (n == -1){
printf("delete failed!");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
|
递归子目录,用unlink和rmdir删除。要看代码,可以看rm的-f选项实现。
|
直接写程序运行linux命令就可以了
rm -rf 目录
这样整个目录就删掉了:)
rm -rf 目录
这样整个目录就删掉了:)
|
有。就是system。
|
直接用system函数调用 rm -rf不就好了。
system("rm -rf /directory");
system("rm -rf /directory");
|
rmdir 删除空目录
unlink 删除文件!
unlink 删除文件!
|
对的。但删除/xxx/sss目录时要先判断目录是否为空!!
|
linux下的c 删除文件和删除文件夹用什么函数
====
我不是写了吗?unlink和rmdir
====
我不是写了吗?unlink和rmdir
|
man