开发者

How do I rename a file?

开发者 https://www.devze.com 2022-12-08 18:20 出处:网络
How can I renam开发者_如何学Pythone a file on unix platform programatically without using the standard rename function?The historical way to rename a file is to use link(2) to create a new hardlink to

How can I renam开发者_如何学Pythone a file on unix platform programatically without using the standard rename function?


The historical way to rename a file is to use link(2) to create a new hardlink to the same file, then to use unlink(2) to remove the old name.


The following is a somewhat ironic solution, that does not use the standard rename(2) system call by itself:

#include <stdlib.h>

if (system("mv file1 file2") != 0)
    perror("system");

It's an indirect usage of rename(2), this syscall is invoked by mv(1).

0

精彩评论

暂无评论...
验证码 换一张
取 消