std::remove

Header: <cstdio>

Deletes the file identified by the character string pointed to by pathname.

# Declarations

int remove( const char* pathname );

# Parameters

# Return value

0 upon success or non-zero value on error.

# Notes

POSIX specifies many additional details for the behavior of this function.

The standard library also defines a function template std::remove taking a pair of iterators and a value, this overload is one of the standard algorithms.

# Example

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
 
int main()
{
    // create a file, check success using operator! of temporary stream object
    if (!std::ofstream("file1.txt").put('a'))
    {
        std::perror("Error creating file1.txt");
        return EXIT_FAILURE;
    }
    std::cout << std::ifstream("file1.txt").rdbuf() << '\n'; // print file
 
    std::remove("file1.txt"); // delete file
 
    if (!std::ifstream{"file1.txt"}) // uses operator! of temporary stream object
    {
        std::perror("Error opening deleted file");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

# See also