Latest web development tutorials

C library functions - remove ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint remove (const char * filename) Remove the given file name filename,so that it is no longer accessible.

statement

Here is the remove () function's declaration.

int remove(const char *filename)

parameter

  • filename - This is a C string containing the name of the file to be deleted.

return value

If successful, it returns zero. If an error, it returns -1 and set errno.

Examples

The following example demonstrates the remove () function is used.

#include <stdio.h>
#include <string.h>

int main ()
{
   int ret;
   FILE *fp;
   char filename[] = "file.txt";

   fp = fopen(filename, "w");

   fprintf(fp, "%s", "这里是 w3cschool.cc");
   fclose(fp);
   
   ret = remove(filename);

   if(ret == 0) 
   {
      printf("文件删除成功");
   }
   else 
   {
      printf("错误:不能删除该文件");
   }
   
   return(0);
}

Suppose we have a text filefile.txt, it reads as follows.We will use the above procedure to delete the file. Let's compile and run the above program, which will generate the following message, and the file is permanently deleted.

文件删除成功

C standard library - <stdio.h> C standard library - <stdio.h>