Syntax
#include <stdio.h> /* also in <io.h> */ int rename(const char *oldname, const char *newname);Description
rename renames the file specified by oldname to the name given by newname. The oldname pointer must specify the name of an existing file. The newname pointer must not specify the name of an existing file. Both oldname and newname must be on the same drive; you cannot rename files across drives. You cannot rename a file with the name of an existing file. You also cannot rename an open file.
rename returns 0 if successful. On an error, it returns a nonzero value.
This example uses rename to rename a file. It issues a message if errors occur.
#include <stdio.h> int main(void) { char *OldName = "oldfile.mjq"; char *NewName = "newfile.mjq"; FILE *fp; fp = fopen(OldName, "w"); fprintf(fp, "Hello world\n"); fclose(fp); if (rename(OldName, NewName) != 0) perror("Could not rename file"); else printf("File \"%s\" is renamed to \"%s\".\n", OldName, NewName); return 0; /**************************************************************************** The output should be: File "oldfile.mjq" is renamed to "newfile.mjq". ****************************************************************************/ }Related Information