Format
#include <direct.h> char *_getcwd(char *pathbuf, int n);
Language Level: Extension
_getcwd gets the full path name of the current
working directory and stores it in the buffer pointed to by pathbuf.
The integer argument n specifies the maximum length for the path
name. An error occurs if the length of the path name, including
the terminating null character, exceeds n
characters.
If the pathbuf argument is NULL, _getcwd uses malloc to reserve a buffer of at least n bytes to store the path name. If the current working directory string is more than n bytes, a large enough buffer will be allocated to contain the string. You can later free this buffer using the _getcwd return value as the argument to free.
Return Value
_getcwd returns pathbuf.
If an error occurs, _getcwd returns NULL and sets errno to one of
the following values:
| Value | Meaning |
| ENOMEM | Not enough storage space available to reserve n bytes (when pathbuf is NULL). |
| ERANGE | The path name is longer than n characters. |
| EOS2ERR | A OS/2 on Windows system call failed. Use _doserrno to obtain more information about the return code. |
Example
This example stores the name of the
current working directory (up to _MAX_PATH characters) in a
buffer. The value of _MAX_PATH is defined in <stdlib.h>.
#include <direct.h> #include <stdio.h> #include <stdlib.h>
int main(void)
{
char buffer[_MAX_PATH];
if (NULL == getcwd(buffer, _MAX_PATH))
perror("getcwd error");
printf("The current directory is %s\n", buffer);
return 0;
/***********************************************
The output should be similar to:
The current directory is E:\MIG_XMPS ***********************************************/ }
![]()
chdir -- Change Current
Working Directory
_fullpath -- Get
Full Path Name of Partial Path
_getdcwd -- Get
Full Path Name of Current Directory
_getdrive -- Get
Current Working Drive
malloc --
Reserve Storage Block
<direct.h>
<stdlib.h>