This blog is under construction

Thursday, 5 April 2012

rename example in C

Header file:

     int rename(const char *oldname, const char *newname);

     Renames the filename from oldname to newname.  Returns 0 on success, non-zero otherwise.

rename function C example:

  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");
                printf("Unable to modify file name\n");
        return 0;

  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

No comments:

Post a Comment