使用C ++代码删除或覆盖文件/文件夹 [英] delete or overwrite a file/folder with C++ codes

查看:232
本文介绍了使用C ++代码删除或覆盖文件/文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
如何使用C ++删除文件/文件夹?

如何使用C ++覆盖文件/文件夹?

谢谢:)

hello
how can I delete a file/folder with C++?

how can I overwrite a file/folder with C++?

thanks :)

推荐答案

(假设使用Windows)

删除:ShFileOperation
覆盖文件:只需创建一个与原始文件同名的文件,并处理文件存在时可能发生的所有错误.
覆盖文件夹:可能与上述相同,但是如果文件夹不为空,则会遇到困难?
(assuming using Windows)

delete : ShFileOperation
overwrite file : just create a file with the same name as the original one and handle all the errors that can happen when the file exists.
overwrite folder : maybe the same thing as above but with difficulty if the folder is not empty?


要删除,请使用以下选项之一:
删除
_tremove(cpFileName)
DeleteFile(cpFileName)

要覆盖文件,请使用以下方式之一以截断模式打开文件:
fopen(cpFileName,_T(" w b"))
_tfopen(cpFileName,_T(" w b"))
:: CreateFile(caFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL, CREATE_ALWAYS ,FILE_ATTRIBUTE_ARCHIVE,NULL);
For Deletion, use one of the following:
remove
_tremove(cpFileName)
DeleteFile(cpFileName)

For overwriting a file, open the file with the truncate mode using one of the following:
fopen(cpFileName,_T("wb"))
_tfopen(cpFileName,_T("wb"))
::CreateFile(caFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,NULL);


还有另一种方法.

在Windows API中.
删除文件:
布尔WINAPI DeleteFile(LPCTSTR lpFileName);

删除文件夹:
布尔WINAPI RemoveDirectory(LPCTSTR lpPathName);

使用标准C库:(便携式)
删除文件:
int __cdecl remove(const char *); //stdio.h或io.h
int __cdecl _unlink(const char *); //stdio.h或io.h

删除文件夹:
int __cdecl rmdir(const char *); //io.h或direct.h

要删除文件或文件夹,必须删除其只读属性,并且不使用它.当然,文件夹必须为空.

要覆盖文件,还需要做更多的工作.它应该被打开以进行写入,并且应该从头到尾都在每个字节上写入一些数据(无论您想要什么).因为删除文件仅将其标记为已删除,并且从逻辑上将其从文件系统中删除,所以属于该文件的所有数据都将保留在磁盘上(直到被另一个磁盘操作覆盖).删除文件并创建一个具有相同名称的新文件可能会(也可能不会)覆盖其目录项,而不是覆盖整个文件数据.

与上述相比,ShFileOperation()的主要优点是能够递归地操作子文件夹.
There are yet another ways of doing these.

In Windows API.
To delete a file:
BOOL WINAPI DeleteFile(LPCTSTR lpFileName);

To remove a folder:
BOOL WINAPI RemoveDirectory(LPCTSTR lpPathName);

Using standard C libs: (portable)
To delete a file:
int __cdecl remove(const char*); // stdio.h or io.h
int __cdecl _unlink(const char*); // stdio.h or io.h

To remove a folder:
int __cdecl rmdir(const char*); // io.h or direct.h

To delete a file or folder, its read-only attribute must be removed and it''s not in use. And folders must be empty, of course.

To overwrite a file needs some more work. It should be opened for writing and should be written from the beginning to the end with some data (whatever you wish) over every byte. Because deleting a file only marks it as deleted and logically removed from the file system, all the data belonging to the file will be retained on disk (up to being overwritten by another disk operation). Deleting a file and creating a new one with the same name may (or may not) write over its directory entry, not over the whole file data.

Main advantage of ShFileOperation() over above is ability to operate recursively into sub folders.


这篇关于使用C ++代码删除或覆盖文件/文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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