删除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 婶婶拜年祝福语大全简短