怎么用C语言删除一个文件夹?

怎么用C语言删除一个文件夹?,第1张

//调用system函数并传递字符串参数rd

/s

/q

path(path为目录的路径)就行了

//下面有一个例子

#include<stdio.h>

#include<string.h>

int

main()

{

char

cmd[256]="rd

/s

/q

"

printf("请输入要删除的目录的路径:")

//将目录的路径连接到cmd的后面

gets(cmd+strlen(cmd))

if(0==system(cmd))

printf("目录已删除,请注意查看!\n")

return

0

}

使用DeleteFile 方法删除指定文件

BOOL DeleteFile(

LPCTSTR lpFileName // 文件名 指针

)

功能说明

删除一个存在的文件

返回值

如果成功返回一个非0值

失败返回0 可以用GetLastError函数得到错误信息

如果程序尝试删除一个不存在的文件。GetLastError返回ERROR_FILE_NOT_FOUND。如果文件是只读 的,则GetLastError返回ERROR_ACCESS_DENIED

示例代码:

CString type,dPath

dPath.Format("%s\\Log\\",SystemDir) //指定路径

BOOL ret=0

CFileFind tempFind

CString foundFileName,tempFileName

CString tempFileFind=dPath+_T("*.*")

BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind)

while(IsFinded)

{

IsFinded=(BOOL)tempFind.FindNextFile()

if(!tempFind.IsDots())

{

foundFileName=tempFind.GetFileName()

tempFileName=dPath+foundFileName

ret = DeleteFile(tempFileName) // 删除文件

}

}

tempFind.Close()

方法1、用文件删除函数如实现:

函数名: remove

功 能: 删除一个文件

用 法: int remove(char *filename)

程序例:

#include <stdio.h>

int main(void)

{

char file[80]

/* prompt for file name to delete */

printf("File to delete: ")

gets(file)

/* delete the file */

if (remove(file) == 0)

printf("Removed %s.\n",file)

else

perror("remove")

return 0

}

方法2、使用 Dos 命令:

system(char *cmd)

其中 cmd 为创建/删除文件的 DOS 命令。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8020005.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存