此函数存在于stdio.h库头文件中。
rename函数的语法如下:
int rename(const char * oldname, const char * newname);
rename()函数的功能它接受两个参数。一个是oldname,另一个是newname。
这两个参数都是指向常量字符的指针,用于定义文件的旧名称和新名称。
如果文件重命名成功,则返回零;否则,返回非零整数。
在重命名操作期间,如果newname文件已经存在,则用这个新文件替换已经存在的文件。
算法参考下面给出的算法,使用rename()函数来更改文件名。
步骤1 - 声明变量
步骤2 - 输入旧文件路径
步骤3 - 输入新文件路径
步骤4 - 检查rename(old, new) == 0
if yes print file renamed successfullyelseunable to rename.
程序以下是使用rename()函数更改文件名的c程序 -
现场演示
#include <stdio.h>int main(){ char old[100], new[100]; printf("enter old file path: "); scanf("%s", old); printf("enter new file path: "); scanf("%s", new); if (rename(old, new) == 0){ printf("file renamed successfully.
"); } else{ printf("unable to rename files
"); } return 0;}
输出当上述程序被执行时,它产生以下结果 −
run 1:enter old file path: test.exeenter new file path: test1.exefile renamed successfully.run 2:enter old file path: priya.center new file path: bhanu.cunable to rename files
以上就是c程序使用rename()函数更改文件名的详细内容。