Header file:
stdio.h
Synopsis:
int rename(const char *oldname, const char *newname);
Description:
Renames the filename from oldname to newname. Returns 0 on success, non-zero otherwise.
rename function C example:
#include<stdio.h>
#include<string.h>
int main() {
char newfile[100], oldfile[100];
int ret = 0;
printf("Enter your old file name:");
fgets(oldfile, 90, stdin);
oldfile[strlen(oldfile) - 1] = '\0';
printf("Enter your new file name:");
fgets(newfile, 90, stdin);
newfile[strlen(newfile) - 1] = '\0';
ret = rename(oldfile, newfile);
if (ret == 0)
printf("File name modified\n");
else
printf("Unable to modify file name\n");
return 0;
}
#include<string.h>
int main() {
char newfile[100], oldfile[100];
int ret = 0;
printf("Enter your old file name:");
fgets(oldfile, 90, stdin);
oldfile[strlen(oldfile) - 1] = '\0';
printf("Enter your new file name:");
fgets(newfile, 90, stdin);
newfile[strlen(newfile) - 1] = '\0';
ret = rename(oldfile, newfile);
if (ret == 0)
printf("File name modified\n");
else
printf("Unable to modify file name\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ls
a.out oldfile.txt rename.c
jp@jp-VirtualBox:~/$ ./a.out
Enter your old file name: oldfile.txt
Enter your new file name: newfile.txt
File name modified
jp@jp-VirtualBox:~/$ ls
a.out newfile.txt rename.c
a.out oldfile.txt rename.c
jp@jp-VirtualBox:~/$ ./a.out
Enter your old file name: oldfile.txt
Enter your new file name: newfile.txt
File name modified
jp@jp-VirtualBox:~/$ ls
a.out newfile.txt rename.c
No comments:
Post a Comment