c rename() 函数是否删除文件?
Does the c rename() function delete files?
我正在练习使用 C 编程语言进行编程,并且正在试验 rename()
函数。我正在使用以下代码:
#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;
}
此代码将名为 "data" 的文件的名称更改为名为 "database" 的文件。我想知道如果您尝试 运行 这段代码会发生什么,但在同一目录中已经有一个名为 "database" 的文件。
这是我在 运行 启用 rename()
函数之前的目录内容:
这是我在 运行 执行 rename()
函数后的目录内容:
看来 rename()
函数确实正确地重命名了我的文件,但它也删除了该目录中已有的同名文件。我想知道 rename()
函数是否就是这样设计的,或者这是否是我的操作系统(Windows 10 - cygwin64 - gcc 编译器)正在做的事情。还有,在使用这个功能的时候,是不是应该先确认没有已经同名的文件,以免被删除?感谢您的帮助和见解。
If new_filename
exists, the behavior is implementation-defined.
例如在 Unix 上,行为似乎是这样的(来自 man rename
):
int
rename(const char *old, const char *new);
[…]
If new
exists, it is first removed.
来自 rename doc :
If oldname is not a directory, then any existing file named newname is
removed during the renaming operation. However, if newname is the name
of a directory, rename fails in this case.
您必须查阅 C 库的文档。根据标准(N1570 7.21.4.2
,强调我的):
The rename
function causes the file whose name is the string pointed to by old
to be
henceforth known by the name given by the string pointed to by new
. The file named
old
is no longer accessible by that name. If a file named by the string pointed to by new
exists prior to the call to the rename function, the behavior is implementation-defined.
以gcc为例rename
:
If oldname is not a directory, then any existing file named newname is removed during the renaming operation. However, if newname is the name of a directory, rename fails in this case.
如果是 VS,however:
The new name must not be the name of an existing file or directory.
我正在练习使用 C 编程语言进行编程,并且正在试验 rename()
函数。我正在使用以下代码:
#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;
}
此代码将名为 "data" 的文件的名称更改为名为 "database" 的文件。我想知道如果您尝试 运行 这段代码会发生什么,但在同一目录中已经有一个名为 "database" 的文件。
这是我在 运行 启用 rename()
函数之前的目录内容:
这是我在 运行 执行 rename()
函数后的目录内容:
看来 rename()
函数确实正确地重命名了我的文件,但它也删除了该目录中已有的同名文件。我想知道 rename()
函数是否就是这样设计的,或者这是否是我的操作系统(Windows 10 - cygwin64 - gcc 编译器)正在做的事情。还有,在使用这个功能的时候,是不是应该先确认没有已经同名的文件,以免被删除?感谢您的帮助和见解。
If
new_filename
exists, the behavior is implementation-defined.
例如在 Unix 上,行为似乎是这样的(来自 man rename
):
int rename(const char *old, const char *new);
[…] If
new
exists, it is first removed.
来自 rename doc :
If oldname is not a directory, then any existing file named newname is removed during the renaming operation. However, if newname is the name of a directory, rename fails in this case.
您必须查阅 C 库的文档。根据标准(N1570 7.21.4.2
,强调我的):
The
rename
function causes the file whose name is the string pointed to byold
to be henceforth known by the name given by the string pointed to bynew
. The file namedold
is no longer accessible by that name. If a file named by the string pointed to bynew
exists prior to the call to the rename function, the behavior is implementation-defined.
以gcc为例rename
:
If oldname is not a directory, then any existing file named newname is removed during the renaming operation. However, if newname is the name of a directory, rename fails in this case.
如果是 VS,however:
The new name must not be the name of an existing file or directory.