C/C++ 中的删除函数
C 库函数 int remove(const char *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 tutorialspoint.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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP