Q:
C program to rename a file using the system() function
belongs to collection: File Handling Examples Programs in C language
File Handling Examples Programs in C language
- C program to find number of lines in a file
- C program to create a text file using file handling, example of fopen, fclose
- C program to create, write and read text in/from file
- C program to print given number of lines of a file (like head command in Linux)
- C program to print contents in reverse order of a file (just like TAC command in Linux)
- C program to compare contents of two files
- C program to copy number of bytes from specific offset to another file
- C - Read Content of a File using getc() using C Program
- C - Convert All Characters in Upper Case of a File using C Program
- C program to delete a specified file using remove() function
- C program to delete a specified file using the system() function
- C program to remove a specified empty directory using the remove() function
- C program to remove an empty directory
- C program to remove a non-empty directory using the system() function
- C program to check a given filename is a directory or not
- C program to remove a specific line from the text file
- C program to replace the specified line in an existing text file
- C program to append the content of one file to the end of another file
- C program to print the list of files of a directory
- C program to print the list of files and subdirectories of the current directory
- C program to check a specified directory exists or not
- C program to check a specified file exists or not
- C program to check a specified file exists or not using the stat() function
- C program to check a specified file exists or not using the access() function
- C program to check a specified file has read, write, and execute permission or not
- C program to create a directory using mkdir() function
- C program to create a directory using system() function
- C program to remove an empty directory using rmdir() function
- C program to rename a file using the rename() function
- C program to rename a file using the system() function
- C program to change the permissions of file using system() function
- C program to capitalize the first letter of every word in a file
Read the old and new filename, and then rename the given file using the system() function by specifying the "mv" command.
The system() function is a library function of <stdlib.h> or <cstdlib> header file which is used to execute the commands that can be executed in the command processor or the terminal of the operating system, and finally returns the command after it has been completed.
Syntax:
The argument string is the command to be executed.
To rename a file, create (form) mv command with old and new file names and pass it into the system() function.
Program:
The source code to rename a file using the system() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
Output:
Explanation:
Here, we created two character arrays oldName, newName. Then we read the old and new names of files from the user. Then we renamed the given file using the system() function by specifying the "mv" command and printed the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer