Skip to content

Instantly share code, notes, and snippets.

@mohamad-wael
Created May 7, 2021 22:19
Show Gist options
  • Save mohamad-wael/482a83badae30dbab8b7e9f3a6c3f48e to your computer and use it in GitHub Desktop.
Save mohamad-wael/482a83badae30dbab8b7e9f3a6c3f48e to your computer and use it in GitHub Desktop.
How to rename a file in C .
#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