C Tutorial – Deleting and Renaming a File

In two previous tutorials we talked about file I/O functions on text files and file I/O functions on binary files. In this C programming language tutorial we look at how to remove files and how to rename a file.

Remove a File

With function remove() we can delete a file. Let us take a look at an example:


	int main(void)
		char buffer[101];

		printf("Name of file to delete:  ");
		gets_s(buffer, 100);

		if(remove(buffer) == 0)
		  printf("File %s  deleted.\n", buffer);
		  fprintf(stderr, "Error deleting the file %s.\n", buffer);

First a buffer is made with room for 100 characters, plus ‘\0’. After the question “Name of file to delete:” is printed on the screen the gets_s is waiting for input of a maximum of 100 characters. (It is also possible to use gets(buffer) but gets_s is more secure, because you can’t overflow the buffer.)

In the “if statement” the function remove is used to remove the file. If the file is successfully deleted then a success message is printed. If something goes wrong (for example the file doesn’t exist) an error message is printed on the standard error stream.

Renaming a File

With the function rename() we can rename a file. Let us take a look at an example:


int main(void)
	char  buffer_old[101], buffer_new[101];

	printf("Current filename: ");
	gets_s(buffer_old, 100);

	printf("New filename: ");
	gets_s(buffer_new, 100);

	if(rename(buffer_old, buffer_new) == 0)
		printf("%s has been rename %s.\n", buffer_old, buffer_new);
		fprintf(stderr, "Error renaming %s.\n", buffer_old);

As in the previous example a buffer for the filename is created of 100 characters, plus a character for ‘\0’.
This time there is also a second buffer created that will hold the new name.

In the “if statement” the function rename() is used. The function rename() needs two variables, one for the old name and one for the new file name. If the renaming either succeeds or fails, a message is printed.

As you can see removing or renaming a file is easy!

That’s all for this C tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 12 responses to “C Tutorial – Deleting and Renaming a File”

Why not let us know what you think by adding your own comment!

  1. Tony on October 4th, 2009:

    This example does not work! Using Dev C++ compliler version as well I receive the following compile error:
    [Linker error] undefined reference to `gets_s’

  2. Tony on October 4th, 2009:

    I got this to work!

    #include <stdio.h>

    int main(void)
    char filename[101], newfilename[101];

    printf (“Please type in name of file to be changed:\n”);
    scanf(“%s”, &filename); /* Get the filename from the keyboard) */

    printf(“You have choosen to rename %s.\nPlease type the new name of the file:\n”, filename);
    scanf(“%s”, &newfilename); /* Get the new filename from the keyboard) */

    if(rename(filename, newfilename) == 0)
    printf(“%s has been rename %s.\n”, filename, newfilename);
    fprintf(stderr, “Error renaming %s.\n”, filename);
    system (“pause”);
    system (“CLS”);

    return 0;

  3. admin on October 4th, 2009:

    I don’t know exact what the problem is (looks your missing an include), but I re-tested the two examples again using VS 2008 (console application) and they worked fine.

    To get your source code example to work I had to add #include <stdlib.h> (for the system(); function), then it also works.

    More info about gets_s and why to use it can be found here

  4. admin on October 5th, 2009:

    My fault, I see now what the problem is: ‘gets_s’ is not part of the C standard, so it depends on your compiler if it is implemented. I use visual studio and they have implemented ‘gets_s’ and it looks like your compiler doesn’t.

  5. Hudiab on February 7th, 2010:

    i want to delete a file directly.plz help me

  6. admin on February 7th, 2010:

    @Hubiab – I don’t know exactly what you mean, because this C language tutorial is about removing files.
    So I make the assumption that you mean without any code around the remove() function. I get this on windows:

    #include <stdio.h>
    void main()

    Hope that this is what you mean.

  7. furqan on November 4th, 2010:

    i confuse how to change file name if i have a file with “c/:desktop/fur.txt”
    destination so how can i rename it plzzzzzzz plzzzzzz help me

  8. jeremy on March 1st, 2013:

    how can i delete and add file in simpliest form of codes

  9. manish on June 7th, 2013:

    how to append multiple file name using c program—–
    write a piece of code which solves my below mentioned purpose——

    let i have a folder named wild consisting
    5 files–

    now using c(file handling concept) write a code to rename those file as—

    animal cat,animal dog,animal horse,animal lion,animal tiger

  10. suleman on September 11th, 2013:

    thanks for the (Renaming a File example) it help me a lot.
    thanks once again

  11. anis on December 15th, 2013:

    Hi, can i know how to delete items in the file?

  12. sadra on January 27th, 2014:

    hi,first thanks alot
    hey,how can i remove an item in the file?