Does c rename () remove files?

I do programming in the C programming language and experimented with a function rename(). I am using the following code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    if(rename ("data", "database") )
    {
        fprintf(stderr, "Can't rename file\n");
        exit(EXIT_FAILURE);
    }
    return 0;
}

This code changes the name of the file named "data" to a file called "database". I was wondering what would happen if you tried to run this code, but already had a file called "database" in the same directory.

This is the contents of the directory that I have before running the function rename():

enter image description here

And this is the contents of the directory that I have after running the function rename():

enter image description here

, rename() , , . , rename(), , (Windows 10 - cygwin64 - gcc-). , , , , ? .

+4
3

C. (N1570 7.21.4.2, ):

rename , , old , , new. old . , , new , .

gcc rename:

oldname , newname . , newname , .

VS, :

.

+7

cppreference.com:

new_filename , .


. Unix ( man rename):

int
rename(const char *old, const char *new);

[...] new, .

+3

From rename document :

If oldname is not a directory, then any existing file named newname is deleted during the rename operation. However, if newname is the name of the directory, renaming is not performed in this case.

0
source

Source: https://habr.com/ru/post/1668203/


All Articles