您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

C程序使用rename()函数更改文件名

2025/6/18 4:40:35发布14次查看
rename函数将文件或目录从旧名称更改为新名称。此操作类似于移动操作。因此,我们也可以使用此rename函数来移动文件。
此函数存在于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()函数更改文件名的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product