Easy Tutorial
❮ C Examples Smallest Array Element C Examples Vowel Consonant ❯

C Library Function - remove()

C Standard Library - <stdio.h>

Description

The C library function int remove(const char *filename) deletes the specified filename filename, so that it is no longer accessible.

Declaration

Here is the declaration for the remove() function.

int remove(const char *filename)

Parameters

Return Value

On success, it returns zero. On error, it returns -1 and sets errno.

Example

The following example demonstrates the use of the remove() function.

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

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

   fp = fopen(filename, "w");

   fprintf(fp, "%s", "这里是 tutorialpro.org");
   fclose(fp);

   ret = remove(filename);

   if(ret == 0) 
   {
      printf("File deleted successfully");
   }
   else 
   {
      printf("Error: unable to delete the file");
   }

   return(0);
}

Assuming we have a text file file.txt with the following content, we will use the above program to delete this file. Let's compile and run the above program, which will produce the following message and the file will be permanently deleted.

File deleted successfully

C Standard Library - <stdio.h>

❮ C Examples Smallest Array Element C Examples Vowel Consonant ❯