源代码

#include <stdio.h>
#include <unistd.h>
# include <iostream>
#include <cstring>
using namespace std;

char *getFilePath(char* buf, int count)
{
    int i;
    int rslt = readlink("/proc/self/exe", buf, count - 1); //将seld改为进程pid就是获取任意进程路径
    if (rslt < 0 || (rslt >= count - 1))
    {
        return NULL;
    }
    buf[rslt] = '\0';
    for (i = rslt; i >= 0; i--)
    {
        if (buf[i] == '/')
        {
            buf[i + 1] = '\0';
            break;
        }
    }
    return buf;
}

int main(){
    char *buf;
    printf("current absolute path:%s\n", strcat(getFilePath(buf, 200), "data.json"));
    return 0;
}

编译

g++ main.cpp -o out