Remove () function in C

The int remove (const char * filename) function in Library C standard deletes the filename so it is no longer accessible.

Declare the remove () function in C

Below is the declaration for the remove () function in C:

 int remove ( const char * filename ) 

Parameters

filename - This is the string containing the file name to be deleted.

Returns the value

If successful, return 0. If there is an error, return -1 and errno are set appropriately.

For example

The following C program illustrates the use of the remove () function in C:

 #include #include int main () { int ret ; FILE * fp ; char filename [] = "baitapc.txt" ; fp = fopen ( filename , "w" ); fprintf ( fp , "%s" , "Hoc C co ban va nang cao tai QTM !!!" ); fclose ( fp ); ret = remove ( filename ); if ( ret == 0 ) { printf ( "Xoa file thanh cong !!!" ); } else { printf ( "Error: khong the xoa file tren" ); } return ( 0 ); } 

Suppose we have baitapc.txt with some content. We are about to delete this file by using the above program. Compiling and running the above program will give the following message and the file has been permanently deleted.

Remove () function in C Picture 1

According to Tutorialspoint

Previous lesson: Function fwrite () in C

Next lesson: rename () function in C

4 ★ | 1 Vote

May be interested

  • Rename () function in CPhoto of Rename () function in C
    the function int rename (const char * old_filename, const char * new_filename) in library c standard makes filename referenced, changed from old_filename to new_filename.
  • The rewind () function in CPhoto of The rewind () function in C
    the void rewind function (file * stream) in standard c library sets the file location to the beginning of the file in the given stream.
  • Function setbuf () in CPhoto of Function setbuf () in C
    the function void setbuf (file * stream, char * buffer) in the standard c library defines how a stream is buffered. this function should be called once the file attached to the stream has been opened, but before any input or output operation has taken place.
  • The function setvbuf () in CPhoto of The function setvbuf () in C
    the function int setvbuf (file * stream, char * buffer, int mode, size_t size) in standard c library determines how a stream should be buffered.
  • Function tmpfile () in CPhoto of Function tmpfile () in C
    function file * tmpfile (void) in standard c library create temporary files in wb + mode. the temporary file created will be automatically deleted when the stream is closed (fclose function) or when the program ends.
  • Function tmpnam () in CPhoto of Function tmpnam () in C
    the function char * tmpnam (char * str) in the c library standard creates and returns a valid temporary file name (temp file) that did not exist before creation. if str is null, it returns the tmp file name.