删除C / C ++中的函数
C库函数intremove(constchar*filename) 删除给定的文件名,使其不再可访问。
以下是remove()函数的声明。
int remove(const char *filename)
此函数使用文件名。这是C字符串,其中包含要删除的文件的名称。成功时,返回零。如果出错,则返回-1,并正确设置errno。
示例
#include <stdio.h>
#include <string.h>
int main () {
int ret;
FILE *fp;
char filename[] = "file.txt";
fp = fopen(filename, "w");
fprintf(fp, "%s", "This is nhooo.com");
fclose(fp);
ret = remove(filename);
if(ret == 0) {
printf("File deleted successfully");
} else {
printf("Error: unable to delete the file");
}
return(0);
}让我们假设我们有一个文本文件file.txt,其中包含一些内容。因此,我们将使用上述程序删除此文件。让我们编译并运行以上程序以产生以下消息,该文件将被永久删除。
输出结果
File deleted successfully