How to Delete a File in C++? Last Updated : 16 May, 2024 Comments Improve Suggest changes Like Article Like Report C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the <stdio.h> header file. The remove() function takes the path of the file as a string argument and deletes the file. Syntax of remove()remove(char * path) where the path is the relative or even absolute path to the file. If the function returns zero, the file has been successfully deleted. If the function returns a non-zero value, an error occurs. C++ Program to Remove a FileThe below example demonstrates how we can remove a file named “myfile.txt” in C++. C++ // C++ program to demonstrate how to remove a file #include <cstdio> #include <iostream> using namespace std; int main() { // Remove the file named "myfile.txt" int status = remove("myfile.txt"); // Check if the file has been successfully removed if (status != 0) { perror("Error deleting file"); } else { cout << "File successfully deleted" << endl; } return 0; } Output File successfully deletedTime Complexity: O(1), as file removal is a constant time operation. Auxilliary Space: O(1) Explanation: The above code attempts to delete the file named "myfile.txt". If file is removed successfully, it prints "File successfully deleted". Otherwise, it prints an error message. Note: The remove() function will not delete a directory. To delete a directory, we need to use the rmdir() function. Also, always ensure that the file is not open in your program or another program before attempting to delete it. Comment More infoAdvertise with us Next Article How to Delete a File in C++? S sonijaiog3d Follow Improve Article Tags : C++ Programs C++ cpp-file-handling C++ File Programs CPP Examples +1 More Practice Tags : CPP Similar Reads How to Delete a Pointer in C++? In C++, memory management is essential to avoid memory leaks, crashes, and undefinable behavior. In this article, we will learn how to properly delete a pointer in C++. Deleting a Pointer in C++By deleting a pointer, we refer to deleting the memory allocated to the pointer. To delete a pointer in C+ 2 min read How to Open and Close a File in C++? In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst 2 min read How to Delete an Element from a Set in C++? A set in C++ is a container that stores unique elements in a sorted order. In this article, we will learn how to delete a specific element from a set. Example: Input: mySet = {5, 2, 8, 1, 4} Element to delete: 2 Output: mySet = {5, 1, 8, 4}Delete an Element from a Set in C++To delete a specific elem 2 min read How to Delete an Element from the Set in C++? In C++, the set container stores unique elements in the sorted order and the deletion operation should not disrupt this sorted order.C++ provides a built-in function set erase(), which can be used to easily delete the given element by passing its value to the function. Let's take a look at the simpl 1 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read C++ Program to Create a File Problem Statement:Write a C++ program to create a file using file handling and check whether the file is created successfully or not. If a file is created successfully then it should print "File Created Successfully" otherwise should print some error message. Approach:Declare a stream class file and 2 min read How to Delete All Elements from a Vector in C++? In C++, you can delete all items from a vector to either clear its contents or free its memory. In this article, we will learn how to delete all items from a vector in C++.The recommended way to delete all items from a vector is by using the vector clear() function. Letâs take a look at a simple exa 2 min read How to Delete an Element from a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to delete a specific element from a multiset. Example: Input: myMultiset = {5, 2, 8, 5, 8, 8}; Element to delete: 8 Output: myMultiset = {5, 2, 5 2 min read Like