Linux中获取当前程序的绝对路径

代码如下:

    char current_absolut_path[MAX_SIZE] = "";
    memset(current_absolut_path,0,MAX_SIZE);
    //获取当前程序绝对路径
    int cnt = readlink("/proc/self/exe", current_absolut_path, MAX_SIZE);
    if (cnt < 0 || cnt >= MAX_SIZE)
    {

    }
    else
    {
        //获取当前目录绝对路径,及去掉程序名
        int i;
        for (i = cnt; i >= 0; --i)
        {
            if (current_absolut_path[i] == '/')
            {
                current_absolut_path[i+1] = '\0';
                break;
            }
        }
    }

 

posted @ 2016-01-06 13:31  随风飘落的雨滴  阅读(661)  评论(0编辑  收藏  举报