rename

Header: <stdio.h>

Changes the filename of a file. The file is identified by character string pointed to by old_filename. The new filename is identified by character string pointed to by new_filename.

# Declarations

int rename( const char* old_filename, const char* new_filename );

# Parameters

# Return value

0 upon success or non-zero value on error.

# Notes

POSIX specifies many additional details on the semantics of this function.

# Example

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* fp = fopen("from.txt", "w"); // create file "from.txt"
    if (!fp)
    {
        perror("from.txt");
        return EXIT_FAILURE;
    }
    fputc('a', fp); // write to "from.txt"
    fclose(fp);
 
    int rc = rename("from.txt", "to.txt");
    if (rc)
    {
        perror("rename");
        return EXIT_FAILURE;
    }
 
    fp = fopen("to.txt", "r");
    if(!fp)
    {
        perror("to.txt");
        return EXIT_FAILURE;
    }
    printf("%c\n", fgetc(fp)); // read from "to.txt"
    fclose(fp);
 
    return EXIT_SUCCESS;
}

# See also