C++ Program to Delete Files

« Previous Program Next Program »

Delete Files in C++

To delete files in C++ programming, you have to ask to the user to enter the file name to delete the file using the function remove().

If the file would be deleted successfully, then it (the function remove()) will return 0 otherwise it will not return 0.

So make a variable say status of int type to initialize remove(filename) and check, whether status contains 0 or not. If it contains 0, then file deleted successfully, otherwise file present (not deleted) as shown here in the following program.

C++ Programming Code to Delete Files

Following C++ program ask to the user to enter the file name which he/she want to delete, then the following program delete the file :

/* C++ Program - Delete Files */
void main()
	int status;
	char fname[20];
	cout<<"Enter name of file, you want to delete : ";
		cout<<"file "<<fname<<" deleted successfully..!!\n";
		cout<<"Unable to delete file "<<fname<<"\n";
		perror("Error Message ");

When the above C++ program is compile and executed, it will produce the following result:

C++ program to delete files

Same Program in Other Language

You may like the same program in other programming languages:

« Previous Program Next Program »


Quick Links
Signup - Login - Give Online Test