這個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