这个Use the readlink() function properly为readlink
功能的正确使用。
如果你有一个std::string
你的路径,你可以做这样的事情:
#include <unistd.h>
#include <limits.h>
std::string do_readlink(std::string const& path) {
char buff[PATH_MAX];
ssize_t len = ::readlink(path.c_str(), buff, sizeof(buff)-1);
if (len != -1) {
buff[len] = '\0';
return std::string(buff);
}
/* handle error condition */
}
如果你只是一个固定的路径之后是:
std::string get_selfpath() {
char buff[PATH_MAX];
ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1);
if (len != -1) {
buff[len] = '\0';
return std::string(buff);
}
/* handle error condition */
}
要使用它:
int main()
{
std::string selfpath = get_selfpath();
std::cout << selfpath << std::endl;
return 0;
}
来源
2011-04-02 20:38:17
Mat
没有,对不起,我想我没有正确短语我一句。我没有路径,我正在使用readlink(“/ proc/self/exe”,buf,bufsize);正确地为了检索它。 – 2011-04-02 21:35:22
我不明白你在说什么。请编辑你的问题,以显示你有什么,以及你想要什么的例子。 – Mat 2011-04-02 21:39:01
我刚刚编辑了一个解释。 – 2011-04-02 21:49:07