std::remove

From cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
Defined in header <cstdio>
int remove( const char* fname );

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

If the file is currently open by this or another process, the behavior of this function is implementation-defined (in particular, POSIX systems unlink the file name, although the file system space is not reclaimed even if this was the last hardlink to the file until the last running process closes the file, Windows does not allow the file to be deleted)

Contents

[edit] Parameters

fname - pointer to a null-terminated string containing the path identifying the file to delete

[edit] Return value

0 upon success or non-zero value on error.

[edit] Notes

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

[edit] Example

#include <iostream>
#include <fstream>
#include <cstdio>
int main()
{
    bool ok = std::ofstream("file1.txt").put('a'); // create file
    if(!ok) { std::perror("Error creating file1.txt"); return 1; }
    std::cout << std::ifstream("file1.txt").rdbuf() << '\n'; // print file
 
    std::remove("file1.txt"); // delete file
 
    ok = std::ifstream("file1.txt");
    if(!ok) { std::perror("Error opening deleted file"); return 1; }
}

Possible output:

a
Error opening deleted file: No such file or directory

[edit] See also

removes a file or empty directory
removes a file or directory and all its contents, recursively
(function)
renames a file
(function)
C documentation for remove