C rename()

The rename() function changes the name of the file specified by oldfname (old file name) to newfname (new file name). The newfname must not match with any existing file name present in the current directory

rename() Syntax

int rename(const char *oldfname, const char *newfname);

The rename() returns zero if successful, otherwise returns non-zero if an error has occurred.

rename() Example

Following c program ask to the user to enter the two file name to change the name of the first file with the second file name:

void main()
	char firstfile[20], secondfile[20];
	printf("Enter first file name : ");
	printf("Enter second file name : ");
	if(rename(firstfile, secondfile) != 0)
		printf("Error occurred in renaming the file..!!\n");
		printf("Press any key to exit..\n");

C Online Test

« Previous Function Next Function »