std::remove(3)
π std::remove(3) β C++ Standard Library, bestie edition
okay so picture this: it's in the C++ Standard Library (that's the std::remove(3) situation, iykyk), and it's your little delete-a-file bestie. Let's get into it β¨
πΈ NAME πΈ
std::remove β literally just std::remove, she's named after herself, iconic behavior honestly π
π Synopsis π
Defined in header <cstdio>, and the whole vibe is:
int remove( const char* pathname );
So what does she do? She deletes the file identified by the character string pointed to by pathname. That's it, that's the gig π¦
β οΈ Now here's the messy part, and you need to hear this one: if the file is currently open by any process, the behavior of this function is implementation-defined. Not "probably fine", not "she'll handle it" β implementation-defined, no cap.
Let me break that down for you:
- On POSIX systems, she unlinks the file name (the directory entry) β but heads up π«Ά the filesystem space used by the file is not reclaimed while it is open in any process and while other hard links to the file exist.
- On Windows, she straight up does not allow the file to be deleted in such cases. She said nope π«
β¨ Parameters β¨
pathnameβ pointer to a null-terminated string containing the path identifying the file to delete. That's your little address label for the file she's about to yeet ποΈ
π Return value π
0upon success π- a non-zero value on error π
π§ Notes π§
- POSIX specifies many additional details for the behavior of this function. Like, a lot of details. Go read those if you're being thorough π
- Plot twist! The standard library also defines a function template
std::removetaking a pair of iterators and a value. This overload is one of the standard algorithms. Yes, same name, different girl, don't mix them up π
π Example π
(the page has a little // Run this code link up top, cute, anyway here's the code fr fr)
#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;
}
β Possible output β
a
Error opening deleted file: No such file or directory
she really said "file? never met her" π
π¦ See also π¦
removeβ removes a file or empty directoryremove_allβ removes a file or directory and all its contents, recursively- (C++17) β (function)
- (C++17)
renameβ renames a file (function)- C documentation for
remove
okay that's the whole page, you ate that π
β¨ so anyway, this little beauty comes to you from http://cppreference.com, version 2024.06.10, and yes the page is literally called std::remove(3) β go delete something responsibly, mwah π«Άππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms