删除目录中的文件时出现问题 [英] Problem in removing the files in a directory

查看:88
本文介绍了删除目录中的文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,我在删除目录中的文件时遇到问题。代码没有错误但是输出总是用于else部分,即文件夹无法删除。这是我的代码。



  #include   <   stdio.h  >  
#include < conio.h >
#include < span class =code-preprocessor> < process.h >
#include < di rh >

void main( )
{
int CheckAgent1;
char * DirectoryName [ 100 ];
system( cls);
printf( 输入目录)的位置;
得到(DirectoryName);
system( dir / p);

CheckAgent1 = rmdir(DirectoryName);
if (CheckAgent1 == 0
{
printf( 成功......);
}
else
{
printf( 无法删除文件夹);
}
getch();
}





请帮助我。

解决方案

< blockquote>如果您正在使用Windows并且正在使用Visiual Studio,则应使用 _rmdir

在任何情况下,该函数都会返回错误代码你可以用来找出出了什么问题。

MSDN:_rmdir [ ^ ]



[更新]

工作解决方案:

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

int RemoveDirectory( const char * szPath);

int main()
{
RemoveDirectory( C:\\Temp \\DbDoc);
return 0 ;
}

int RemoveDirectory( const char * pszPath)
{
char szSearchPath [ 300 ] = ; // 假设路径最大为260字节
strncpy_s(szSearchPath,pszPath, sizeof (szSearchPath) - 1 );
strncat_s(szSearchPath, \\ *。* sizeof (szSearchPath) - 1 );

struct _finddata_t fileinfo;
intptr_t fh = _findfirst(szSearchPath,& fileinfo);

if (fh == - 1
{
return errno;
}

char szSubPath [ 300 ] = ; // 假设路径最大为260字节

int retVal = 0 ;
bool isDir = false ;
int error = 0 ;
while (retVal!= - 1
{
strncpy_s (szSubPath,pszPath, sizeof (szSubPath) - 1 );
strncat_s(szSubPath, \\ sizeof (szSubPath) - 1 );
strncat_s(szSubPath,fileinfo.name, sizeof (szSubPath) - 1 );

isDir =((fileinfo.attrib& 0x10)== 0x10);
if (isDir)
{
if ((strcmp( fileinfo.name, )!= 0 )&&(strcmp(fileinfo.name, ..)!= 0 ))
{
error = RemoveDirectory(szSubPath);
// 执行错误
}
}
else
{
// 删除文件
remove(szSubPath);
}
retVal = _findnext(fh,& fileinfo);
}
_findclose(fh);

if (error == 0
_rmdir(pszPath );
}



我没有长时间编程C,所以我没有特定的文件命令。

你应该从上面的伪代码中得到基本的想法。



有关列出文件和目录的方法,请参阅此链接:

如何使用C或C ++获取目录中的文件列表? [ ^ ]


Sir, I have a problem in removing the files in a directory.The code has no errors however the output always goes for the else part i.e., Folder cannot be deleted.Here is my code.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dir.h>

void main()
{
    int CheckAgent1;
    char *DirectoryName[100];
    system("cls");
    printf("Enter the location of the directory");
    gets(DirectoryName);
    system("dir/p");
    
    CheckAgent1 = rmdir(DirectoryName);
    if (CheckAgent1==0)
    {
        printf("Succeeded...");
    }
    else
    {
        printf("Failed to delete the folder");
    }
    getch();
}



Kindly, help me with this.

解决方案

If you are using Windows and are working with Visiual Studio you should use _rmdir instead.
In any case the function returns an error code that you can use to find out what is going wrong.
See MSDN: _rmdir[^]

[UPDATE]
Working solution:

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

int RemoveDirectory(const char* szPath);

int main()
{
    RemoveDirectory("C:\\Temp\\DbDoc");
    return 0;
}

int RemoveDirectory(const char* pszPath)
{
    char szSearchPath[300] = "";	// Assuming the path is max 260 bytes
    strncpy_s(szSearchPath, pszPath, sizeof(szSearchPath)-1);
    strncat_s(szSearchPath, "\\*.*", sizeof(szSearchPath) - 1);
    
    struct _finddata_t fileinfo;
    intptr_t fh = _findfirst(szSearchPath, &fileinfo);
    
    if (fh == -1)
    {
    	return errno;
    }
    
    char szSubPath[300] = "";		// Assuming the path is max 260 bytes
    
    int retVal = 0;
    bool isDir = false;
    int error = 0;
    while (retVal != -1)
    {
    	strncpy_s(szSubPath, pszPath, sizeof(szSubPath) - 1);
    	strncat_s(szSubPath, "\\", sizeof(szSubPath) - 1);
    	strncat_s(szSubPath, fileinfo.name, sizeof(szSubPath) - 1);
    
    	isDir = ((fileinfo.attrib & 0x10) == 0x10);
    	if (isDir)
    	{
            if ((strcmp(fileinfo.name, ".") != 0) && (strcmp(fileinfo.name, "..") != 0))
            {
		error = RemoveDirectory(szSubPath);
		// Do something with the error
            }
    	}
    	else
    	{
    		// Remove file
    		remove(szSubPath);
    	}
    	retVal = _findnext(fh, &fileinfo);
    }
    _findclose(fh);
    
    if (error == 0)
    	_rmdir(pszPath);
}


I haven't programmed C in a long time, so I don't have the specific file commands on top of my head.
You should get the basic idea from the "pseudo" code above, though.

See this link for a way to list the files and directories:
How can I get the list of files in a directory using C or C++?[^]


这篇关于删除目录中的文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆