std::remove(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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 πŸ’–

  • 0 upon 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::remove taking 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 directory
  • remove_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