gpt4 book ai didi

c - 为什么程序退出后rmdir会删除文件夹?(在Win7中使用Dev C++)

转载 作者:行者123 更新时间:2023-11-30 15:33:48 29 4
gpt4 key购买 nike


我正在尝试编写一个名为 myremoved 的函数。我希望它删除一个非空文件夹。
但现在我遇到了一个问题,当我使用函数(下面的代码)删除所有文件和文件夹时,“rmdir”表示文件夹已删除,但是当我打开 Windows 资源管理器时,我看到已报告删除的文件夹仍然存在(我尝试访问,但被拒绝。)。

这是函数“myremove”代码:

int myremove(char *dirname){
/*
myremove(Folder name):Remove all files and folders in that.
*/
DIR *folder;
struct _finddata_t c_file;
char str[256];
long hasfile;
int a,result=0;

_chdir(dirname);
hasfile =_findfirst("*",&c_file);
if(hasfile!=-1){
do{
str[0]='\0';
strcat(str,c_file.name);
if((strcmp(str,".")!=0)&&(strcmp(str,"..")!=0)) {
//Is it a folder or not
if ((folder = opendir(c_file.name)) == NULL) {
//Not a folder,using remove to delete it
if((a=remove(c_file.name))==0){
printf("Remove: %s OK.\n",c_file.name);
}else{
result=-1;
printf("Remove: %s Failed.\n",c_file.name);
}
}else{
closedir(folder);
if((a=myremove(c_file.name))!=0){result=-1;}
}
}
}while( _findnext( hasfile, &c_file )==0 );
}
_chdir("..");
if ((folder = opendir(dirname)) != NULL){
if((a=rmdir(dirname))!=0){
result=-1;
printf("Remove: %s Failed.\n",c_file.name);
}else{
printf("Remove: %s OK.\n",c_file.name);
}
}
return result;
}

int main()
{
char dirname[256],ch;
printf("Please enter the folder name which you want to remove:");
gets(dirname);
if (strlen(dirname)!=0){
printf("Notice:It will remove %s folder and all things in this folder,sure to continue[Y/N]?",dirname);
scanf("%c",&ch);
if(ch=='Y'){
if (myremove(dirname)==0){
printf("Folder %s Removed.\n",dirname);
}else{
if (rmdir(dirname)==0){
printf("Folder %s Removed.\n",dirname);
}else{
printf("Folder %s couldn't be removed.\n",dirname);
}

}
}else if (ch=='N'){
printf("User cancel, program will exit.\n");
}
}else{
printf("Nothing input,program will exit.\n");
}
system("pause");
return 0;
}

最佳答案

好吧,我自己解决了这个问题。
因为我使用了 _findfirst_findnext,但忘记使用 _findclose
所以该文件夹一直被锁定直到程序结束。
谢谢帮助。

#include <stdio.h>
#include <string.h>
#include <dirent.h>


int myremove(char *dirname){
DIR *folder;
struct _finddata_t c_file;
char str[256];
long hasfile;
int a,result=0;

_chdir(dirname);
hasfile =_findfirst("*",&c_file);
if(hasfile!=-1){
do{
str[0]='\0';
strcat(str,c_file.name);
if((strcmp(str,".")!=0)&&(strcmp(str,"..")!=0)) {
if ((folder = opendir(c_file.name)) == NULL) {
if((a=remove(c_file.name))==0){
printf("Remove: %s OK.\n",c_file.name);
}else{
result=-1;
printf("Remove: %s Failed.\n",c_file.name);
}
}else{
closedir(folder);
if((a=myremove(c_file.name))!=0){result=-1;}
}
}
}while( _findnext( hasfile, &c_file )==0 );
_findclose(hasfile);
}
_chdir("..");
if((a=rmdir(dirname))!=0){
result=-1;
printf("Remove: %s Failed.\n",dirname);
}else{
printf("Remove: %s OK.\n",dirname);
}
return result;
}

int main( int argc, char *argv[])
{
char dirname[256],cmmd[20],ch;
printf("Please enter folder name which you want to remove:");
gets(dirname);
if (strlen(dirname)!=0){
printf("Notice:Now start to %s delete action, sure to continue[Y/N]?",dirname);
scanf("%c",&ch);
if(ch=='Y'){
if (myremove(dirname)==0){
printf("Folder %s Removed.\n",dirname);
}else{
printf("Folder %s Remove failed.\n",dirname);
}
}else if (ch=='N'){
printf("User cancel, program will exit...\n");
}
}else{
printf("Input empty, program will exit...\n");
}
system("pause");
return 0;
}

关于c - 为什么程序退出后rmdir会删除文件夹?(在Win7中使用Dev C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23582608/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com