[英]How to make ifstream follow symbolic link?
In my C++ program I'm using ifstream("/etc/mtab", ios::ate|ios:in)
.在我的 C++ 程序中,我使用
ifstream("/etc/mtab", ios::ate|ios:in)
。 An year back the program ran good but now ifstream::is_open()
is returning false.一年前,程序运行良好,但现在
ifstream::is_open()
返回 false。 By digging I found /etc/mtab
is a symbolic link to /proc/mounts
通过挖掘我发现
/etc/mtab
是/proc/mounts
的符号链接
--Update-- - 更新 -
/proc/mounts
is again a symbolic link to /proc/self/mounts
which is actual file. /proc/mounts
再次是指向/proc/self/mounts
的符号链接,它是实际文件。 I couldn't succeed opening this thought I'm running the program as a root user.我无法成功打开这个以为我以 root 用户身份运行程序的想法。 So may be ifstream is following the symbolic link and is not able to open
/proc/self/mounts
所以可能是 ifstream 跟随符号链接并且无法打开
/proc/self/mounts
although not directly answering on how to resolve symbolic links with std::fstream
, a filesystem library was introduced by C++ 17 (was not available at the time this question was opened)尽管没有直接回答如何使用
std::fstream
解析符号链接,但C++ 17 引入了一个文件系统库(在打开此问题时不可用)
it can deal with symlinks by:它可以通过以下方式处理符号链接:
std::filesystem::read_symlink
std::filesystem::read_symlink
std::filesystem::copy_symlink
std::filesystem::copy_symlink
std::filesystem::create_symlink
std::filesystem::create_symlink
std::filesystem::is_symlink
std::filesystem::is_symlink
note that a filesystem library is also available in boost请注意,文件系统库也可用于 boost
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.