In this program, we will delete a specified file using the system() function by passing the "rm" command.
Program:
The source code to delete a specified file using the system() function is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to delete a specified file
// using system() library function
#include <stdio.h>
#include <stdlib.h>
int main()
{
char fileName[] = "includehelp.txt";
char cmd[32] = { 0 };
int ret = 0;
sprintf(cmd, "rm %s", fileName);
ret = system(cmd);
if (ret == 0)
printf("File deleted successfully\n");
else
printf("Unable to delete file %s\n", fileName);
return 0;
}
Output:
File deleted successfully
Explanation:
In the main() function, we created a character array filename, which is initialized with "inclludehelp.txt". Then we used the sprintf() function to prepare the "rm" command and executed the command using the system() function. The function returns 0 when the specified file gets deleted, otherwise, it returns a non-zero value. After that, we printed the appropriate message on the console screen.
In this program, we will delete a specified file using the system() function by passing the "rm" command.
Program:
The source code to delete a specified 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:
In the main() function, we created a character array filename, which is initialized with "inclludehelp.txt". Then we used the sprintf() function to prepare the "rm" command and executed the command using the system() function. The function returns 0 when the specified file gets deleted, otherwise, it returns a non-zero value. After that, we printed the appropriate message on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer