rename()

Changes the name of a file.

  #include <stdio.h>
  
  int rename(const char *old, const char *new);    
Parameter

old

The old file name.

new

The new file name.

Remarks

The rename() function changes the name of a file, specified by old to the name specified by new. rename() returns a nonzero if it fails and returns zero if successful.

Listing: Example of rename() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

char oldname[50];

char newname[50];

// get the current filename from the user

printf("Please enter the current file name.\n");

gets(oldname);

// get the new filename from the user

printf("Please enter the new file name.\n");

gets(newname);

// rename oldname to newname

if (rename(oldname, newname) != 0) {

printf("Can't rename %s to %s.\n", oldname,

newname);

exit(1);

}

return 0;

}

Output:

Please enter the current file name.

boots.txt

Please enter the new file name.

sandals.txt