ProAnswers.org

In C/C++ How to obtain the full path of current directory?

C/C++: How to obtain the full path of current directory?

1 Like

below is the code for obtaining full path of current directory

for Windows:

int bytes = GetModuleFileName(NULL, pBuf, len);
if(bytes == 0)
return -1;
else
return bytes;

for Linux:

char szTmp[32];
sprintf(szTmp, “/proc/%d/exe”, getpid());
int bytes = MIN(readlink(szTmp, pBuf, len), len - 1);
if(bytes >= 0)
pBuf[bytes] = ‘\0’;
return bytes;