_getcwd、_wgetcwd

获取当前的工作目录。

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。有关详细信息,请参见 CRT functions not supported with /ZW(CRT 函数不支持使用/ZW)。

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

参数

  • buffer
    路径的存储位置。

  • maxlen
    路径的最大长度的字符:_getcwd 的 _wgetcwd和 char 的 wchar_t。

返回值

返回指向 buffer 的指针。 NULL 返回值指示错误,并且errno 设置为任一对 ENOMEM,指示分配 没有足够的内存分配maxlen 字节 (当 NULL 参数是作为 buffer) 时,或对于 ERANGE,指示路径比 maxlen 字符更长。 如果 maxlen 小于或等于零,此函数调用无效参数处理程序,就如 参数验证所述。

有关这些属性和其他的更多信息返回代码示例,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

备注

_getcwd 函数获取当前工作目录的完整路径作为默认的驱动程序并将其存储在 buffer。 整数参数 maxlen 对路径指定最大的长度。 如果路径的长度 (包括 null 终止字符) 超过 maxlen*.*,则会产生错误buffer 参数可以为 NULL;缓冲区的最少大小 maxlen (仅限如果需要) 自动指派,使用 malloc存储路径。 此缓冲区可以通过调用 free 后释放,并将传递其 _getcwd 返回值 (对于分配缓冲区的指针)。

_getcwd 返回表示当前工作目录路径的字符串。 如果当前工作目录是根目录,则该字符串以反斜杠 ( \ ) 结束。 如果当前工作目录是除根目录的目录,该字符串以该目录名结束,而不是以斜杠。

_wsetlocale_wgetcwd 是 _getcwd 的宽字符版本,buffer 参数和_wgetcwd 的返回值都是宽字符字符串。 _wgetcwd 和 _getcwd 行为相同,否则。

当定义 _DEBUG 和 _CRTDBG_MAP_ALLOC 时,对 _getcwd 和 _wgetcwd 的调用被 _getcwd_dbg 和 _wgetcwd_dbg 的调用所替换用来允许调试内存分配。 有关详细信息,请参阅 _getcwd_dbg,_wgetcwd_dbg

一般文本例程映射

Tchar.h 例程

未定义 _UNICODE 和 _MBCS

已定义 _MBCS

已定义 _UNICODE

_tgetcwd

_getcwd

_getcwd

_wgetcwd

要求

例程

必需的标头

_getcwd

<direct.h>

_wgetcwd

<direct.h> or <wchar.h>

有关兼容性的更多信息,请参见兼容性

示例

// 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>
#include <stdlib.h>
#include <stdio.h>

int main( void )
{
   char* buffer;

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

.NET Framework 等效项

System::Environment::CurrentDirectory

请参见

参考

目录控制

_chdir、_wchdir

_mkdir、_wmkdir

_rmdir、_wrmdir