std::filesy...read_symlink(3)
Hey bestie!! π Welcome to std::filesy...read_symlink(3) β the C++ Standard Library manual page for std::filesystem::read_symlink. Lowkey one of those pages that's like βfollow the link, but make it filesystem,β and weβre gonna get through it together, fr fr. π¦
π NAME π
std::filesystem::read_symlink β std::filesystem::read_symlink (yes, itβs the same name twice, itβs giving βI contain multitudesβ π
).
β¨ Synopsis β¨
Okay so, first things first: itβs defined in the header <filesystem>. Hereβs the synopsis exactly as the page serves it, bestie:
Defined in header <filesystem>
std::filesystem::path read_symlink( const std::filesystem::path& p
(1) (since C++17)
);
std::filesystem::path read_symlink( const std::filesystem::path&
p,
(2) (since C++17)
std::error_code& ec );
So the vibe is: if the path p refers to a symbolic link, returns a new path object which refers to the target of that symbolic link. β¨
π¨ It is an error if p does not refer to a symbolic link. The non-throwing overload returns an empty path on errors.
π Parameters π
pβ path to a symlink. Thatβs the linky-link youβre asking about. πecβ out-parameter for error reporting in the non-throwing overload. Itβs where the error code goes to spill the tea. β
πΈ Return value πΈ
The target of the symlink (which may not necessarily exist). So like, donβt assume the target is actually there, bestie β it might be giving ghost town. π»
π Exceptions π
Any overload not marked noexcept may throw std::bad_alloc if memory allocation fails. π
- Throws
std::filesystem::filesystem_erroron underlying OS API errors, constructed withpas the first path argument and the OS error code as the error code argument. - Sets a
std::error_code¶meter to the OS API error code if an OS API call fails, and executesec.clear()if no errors occur.
π§ͺ Example π§ͺ
// Run this code
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main()
{
for (fs::path p : {"/usr/bin/gcc", "/bin/cat", "/bin/mouse"})
{
std::cout << p;
fs::exists(p) ?
fs::is_symlink(p) ?
std::cout << " -> " << fs::read_symlink(p) << '\n' :
std::cout << " exists but it is not a symlink\n" :
std::cout << " does not exist\n";
}
}
π Possible output π
"/usr/bin/gcc" -> "gcc-5"
"/bin/cat" exists but it is not a symlink
"/bin/mouse" does not exist
π See also π
is_symlink(C++17) β checks whether the argument refers to a symbolic link (function).create_symlink(C++17),create_directory_symlink(C++17) β creates a symbolic link (function).copy_symlink(C++17) β copies a symbolic link (function).status(C++17) β determines file attributes (function).symlink_status(C++17) β determines file attributes, checking the symlink target (function).
Source/version: http://cppreference.com Β· date: 2024.06.10 β bye bestie, go read those symlinks and stay iconic π
π¦β¨
π¦ 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