删除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
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短