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:
#include<stdio.h>
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);
else
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:
#include<stdio.h>
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);
}
else
{
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 example does not work! Using Dev C++ compliler version 4.9.9.2 as well I receive the following compile error:
[Linker error] undefined reference to `gets_s’
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);
}
else
{
fprintf(stderr, “Error renaming %s.\n”, filename);
}
system (“pause”);
system (“CLS”);
return 0;
}
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
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.
i want to delete a file directly.plz help me
@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()
{
remove(“c:/test.txt”);
}
Hope that this is what you mean.