How do I get the directory that a program is running from?

后端 未结 23 1647
温柔的废话
温柔的废话 2020-11-22 04:39

Is there a platform-agnostic and filesystem-agnostic method to obtain the full path of the directory from where a program is running using C/C++? Not to be confused with the

23条回答
  •  粉色の甜心
    2020-11-22 05:14

    Here's code to get the full path to the executing app:

    Windows:

    int bytes = GetModuleFileName(NULL, pBuf, len);
    return bytes ? bytes : -1;
    

    Linux:

    int bytes = MIN(readlink("/proc/self/exe", pBuf, len), len - 1);
    if(bytes >= 0)
        pBuf[bytes] = '\0';
    return bytes;
    

提交回复
热议问题