Delete a Specific File Using C++

In this exercise, you will write a C++ program that deletes a specific file from the system using file handling operations. Managing files programmatically is an essential skill in software development, especially for building file maintenance or automation tools. This program will prompt the user to input the filename, check if the file exists, and then attempt to remove it using the standard `remove()` function from the C++ Standard Library. It includes error checking to inform the user whether the file was successfully deleted or if an issue occurred (such as the file not being found).

Group

File Management in C++

Objective

1. Prompt the user to enter the name of the file to be deleted.
2. Use the `remove()` function to attempt to delete the file.
3. Check the return value of `remove()` to determine if the operation succeeded.
4. Display a message indicating whether the file was deleted or if an error occurred.

Create a program that deletes a specific file.

Example C++ Exercise

 Copy C++ Code
#include <iostream>     // For input and output
#include <cstdio>       // For using the remove() function
#include <string>       // For using the string class
using namespace std;

int main() {
    string filename; // Variable to store the name of the file to delete

    // Ask the user to enter the filename
    cout << "Enter the name of the file to delete: ";
    getline(cin, filename); // Read the full filename from user input

    // Attempt to delete the file using the remove() function
    if (remove(filename.c_str()) == 0) {
        // If remove() returns 0, the file was successfully deleted
        cout << "File deleted successfully." << endl;
    } else {
        // If remove() returns a non-zero value, an error occurred
        cout << "Error: Unable to delete the file. It may not exist." << endl;
    }

    return 0; // End of program
}

 Output

//Output Example:
Enter the name of the file to delete: example.txt
File deleted successfully.

//Or in case of error:
Enter the name of the file to delete: missing.txt
Error: Unable to delete the file. It may not exist.

Share this C++ Exercise


More C++ Programming Exercises of File Management in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • ATM Transaction Logger in C++

    In this exercise, you will develop a simple ATM simulator in C++ that logs every transaction to a file. Logging is a vital component in real-world applications, especially in banki...

  • Read and Display a Text File Using C++

    In this C++ exercise, you will create a program that reads the contents of a text file and prints them to the console. This task demonstrates how to work with file input streams in...

  • Register User Information and Save It to a File in C++

    In this C++ exercise, you will create a program that collects basic user information—specifically, their name and age—and writes it into a text file. This task introduces you to fi...

  • Count Words in a Text File Using C++

    In this exercise, you will write a C++ program that reads a text file and counts the total number of words contained in it. This task will help you become familiar with file input ...

  • Copy File Contents Using C++

    This exercise involves writing a C++ program to copy the entire content of one file into another. By completing this task, you will practice using file streams in C++, specifically...

  • Find and Replace a Word in a File Using C++

    In this exercise, you will create a C++ program that searches for a specific word in a text file and replaces it with another word. This task will help you strengthen your understa...