Created
May 7, 2021 22:19
-
-
Save mohamad-wael/482a83badae30dbab8b7e9f3a6c3f48e to your computer and use it in GitHub Desktop.
How to rename a file in C .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
/* A simple program demonstrating the | |
C standard library , rename function . | |
The rename function can rename files | |
and directories .*/ | |
int | |
main | |
(int argc , char * argv [ ] ){ | |
char * prog_name = argv [0 ]; | |
char * from; | |
char * to; | |
if (argc != 3 ){ | |
fprintf(stderr , "Usage: %s from to.\n" , prog_name ); | |
exit (EXIT_FAILURE ); } | |
from = argv [1 ]; | |
to = argv [2 ]; | |
if (rename (from , to ) == 0 ){ | |
printf ("The file %s , was renamed to %s\n" , from , to ); | |
return 0; } | |
else { | |
perror (NULL ); | |
exit (EXIT_FAILURE ); } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment