std::rename(3)
π NAME π
std::rename - std::rename β okay so the name is literally std::rename and the one-liner is also std::rename. Minimalist queen behavior. This is the std::rename(3) page from the C++ Standard Libary manual, section 3, and we're gonna get you comfy with it, bestie π«Ά.
β¨ Synopsis β¨
Defined in header <cstdio>.
int rename( const char* old_filename, const char* new_filename );
So what does it do? It changes the filename of a file. The file is identified by the character string pointed to by old_filename. The new filename is identified by the character string pointed to by new_filename.
And if new_filename already exists? Behavior is implementation-defined β so not universal, check your implementation, no cap. Implementation-defined is the vibe check you don't skip.
π Parameters π
old_filenameβ a pointer to a null-terminated string containing the path identifying the file to rename. That's your "before" path, bestie.new_filenameβ a pointer to a null-terminated string containing the new path of the file. That's your "after" path, slay.
π Return value π
Returns 0 upon success, or a non-zero value on error. That's the whole story. 0 = iconic, non-zero = something went off.
π¦ Notes π¦
POSIX specifies many additional details on the semantics of this function, and those details are reproduced in C++ by std::filesystem::rename. So if you want the full deep lore, that's your next stop, iykyk.
π₯ Example π₯
Here's the example, literally verbatim. It even starts with // Run this code because that's what the page says, bestie.
// Run this code
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
if (!std::ofstream("from.txt").put('a')) // create and write to file
{
std::perror("Error creating from.txt");
return EXIT_FAILURE;
}
if (std::rename("from.txt", "to.txt"))
{
std::perror("Error renaming");
return EXIT_FAILURE;
}
std::cout << std::ifstream("to.txt").rdbuf() << '\n'; // print file
return EXIT_SUCCESS;
}
β Output β
And the output is literally:
a
π See also π
renameβ moves or renames a file or directory (C++17) (function)removeβ erases a file (function)- C documentation for
rename
That's the page, bestie! Shoutout to the source http://cppreference.com, the version/date 2024.06.10, and this very iconic std::rename(3) page. 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