Rediģēt

Kopīgot, izmantojot


_getcwd, _wgetcwd

Gets the current working directory.

Syntax

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

Parameters

buffer
Storage location for the path.

maxlen
Maximum length of the 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's 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. If maxlen is less than or equal to zero, this function invokes an invalid parameter handler, as described in Parameter validation.

For more information about these and other return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

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) is automatically 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.

When _DEBUG and _CRTDBG_MAP_ALLOC are defined, calls to _getcwd and _wgetcwd are replaced by calls to _getcwd_dbg and _wgetcwd_dbg, to allow you to debug memory allocations. For more information, see _getcwd_dbg, _wgetcwd_dbg.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

Tchar.h routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_tgetcwd _getcwd _getcwd _wgetcwd

Requirements

Routine Required header
_getcwd <direct.h>
_wgetcwd <direct.h> or <wchar.h>

For more compatibility information, see Compatibility.

Example

// crt_getcwd.c
// Compile with: cl /W4 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. Passing NULL as the buffer forces getcwd to allocate
// memory for the path, which allows the code to support file paths
// longer than _MAX_PATH, which are supported by NTFS.

#include <direct.h> // _getcwd
#include <stdlib.h> // free, perror
#include <stdio.h>  // printf
#include <string.h> // strlen

int main( void )
{
   char* buffer;

   // Get the current working directory:
   if ( (buffer = _getcwd( NULL, 0 )) == NULL )
      perror( "_getcwd error" );
   else
   {
      printf( "%s \nLength: %zu\n", buffer, strlen(buffer) );
      free(buffer);
   }
}
C:\Code

See also

Directory control
_chdir, _wchdir
_mkdir, _wmkdir
_rmdir, _wrmdir