Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

_getcwd, _wgetcwd

Get the current working directory.

char *_getcwd( 
   char *buffer,
   int maxlen 
);
wchar_t *_wgetcwd( 
   wchar_t *buffer,
   int maxlen 
);

Parameters

buffer
Storage location for path.
maxlen
Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd.

Return Value

Returns a pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these and other return codes.

Remarks

The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).

_getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash.

_wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tgetcwd _getcwd _getcwd _wgetcwd

Requirements

Routine Required header Compatibility
_getcwd <direct.h> Win 98, Win Me, Win NT, Win 2000, Win XP
_wgetcwd <direct.h> or <wchar.h> Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// crt_getcwd.c
/* This program places the name of the current directory in the 
 * buffer array, then displays the name of the current directory 
 * on the screen. Specifying a length of _MAX_PATH leaves room 
 * for the longest legal path name.
 */

#include <direct.h>
#include <stdlib.h>
#include <stdio.h>

int main( void )
{
   char buffer[_MAX_PATH];

   /* Get the current working directory: */
   if( _getcwd( buffer, _MAX_PATH ) == NULL )
      perror( "_getcwd error" );
   else
      printf( "%s\n", buffer );
}

Sample Output

C:\Code

See Also

Directory Control Routines | _chdir | _mkdir | _rmdir | Run-Time Routines and .NET Framework Equivalents

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.