Deleting File in C++

Deleting File in C++ programming by using remove() function to delete any file from the computer. remove() function takes one string parameter, the parameter will be the path of the file.

Program Deleting File

Program

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int status;
cout<<"Deleting file..."<<endl;
status = remove("C://test/MyTxtFile.txt"); //deleting file.
if( status == 0 ) //if delete then
cout<<"File deleted successfully."<<endl;
else //if not then
{
cout<<"Unable to delete the file."<<endl;
perror("Error"); //'perror' used for printing About error.
}
getch();
}
Output
Deleting file...
File deleted successfully.

Program Deleting User Entered File Name File

Program

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int status;
char file_name[25];
cout<<"Enter the name of file you wish to delete: ";
gets(file_name); //user enter file name storing in 'file_name'.
cout<<"Deleting file..."<<endl;
status = remove(file_name); //deleting.
if( status == 0 ) //if delete then
cout<<file_name<<" file deleted successfully."<<endl;
else //if not then
{
cout<<"Unable to delete the file"<<endl;
perror("Error"); //'perror' used for printing About error.
}
getch();
}
Output
Enter the name of file you wish to delete: C://test/MyTxtFile2.txt
Deleting file...
C://test/MyTxtFile2.txt file deleted successfully.