重命名 C/C++ 函数
C 库函数 int rename(const char *old_filename, const char *new_filename) 会将由 old_filename 引用文件名更改为 new_filename
以下是 rename() 函数的声明。
int rename(const char *old_filename, const char *new_filename)
参数 old_filename − 这是包含要重命名和/或移动的文件名的 C 字符串,new_filename − 这是包含文件新名称的 C 字符串。
成功时,会返回零。如果出错,则返回 -1,且 errno 会被适当地设置。
示例
#include <stdio.h>
int main () {
int ret;
char oldname[] = "file.txt";
char newname[] = "newfile.txt";
ret = rename(oldname, newname);
if(ret == 0) {
printf("File renamed successfully");
} else {
printf("Error: unable to rename the file");
}
return(0);
}我们假设有一个文本文件 file.txt,其中有一些内容。因此,我们将使用上述程序重命名此文件。我们编译并运行上述程序,生成以下消息,并且该文件将重命名为 newfile.txt 文件。
输出
File renamed successfully
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP