I'm making a C program where I need to get the directory that the program is started from. This program is written for UNIX computers. I've been looking at opendir()
and telldir()
, but telldir()
returns a off_t (long int)
, so it really doesn't help me.
How can I get the current path in a string (char array)?
Note that
getcwd(3)
is also available in Microsoft's libc: getcwd(3), and works the same way you'd expect.Must link with
-loldnames
(oldnames.lib, which is done automatically in most cases), or use_getcwd()
. The unprefixed version is unavailable under Windows RT.Although the question is tagged Unix, people also get to visit it when their target platform is Windows, and the answer for Windows is the
GetCurrentDirectory()
function:These answers apply to both C and C++ code.
Link suggested by user4581301 in a comment to another question, and verified as the current top choice with a Google search 'site:microsoft.com getcurrentdirectory'.
Look up the man page for
getcwd
.Have you had a look at
getcwd()
?Simple example: