目录

int remove(const char *filename)

描述 (Description)

C库函数int remove(const char *filename)删除给定的filename ,使其不再可访问。

声明 (Declaration)

以下是remove()函数的声明。

int remove(const char *filename)

参数 (Parameters)

  • filename - 这是包含要删除的文件名的C字符串。

返回值 (Return Value)

成功时,返回零。 出错时,返回-1,并正确设置errno。

例子 (Example)

以下示例显示了remove()函数的用法。

#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 iowiki.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
↑回到顶部↑
WIKI教程 @2018