_getcwd, _wgetcwd

現在の作業ディレクトリを取得します。

構文

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

パラメーター

buffer
パスの格納場所。

maxlen
文字数でのパスの最大長。char の場合は _getcwd、および wchar_t の場合は _wgetcwd

戻り値

bufferへのポインターを返します。 NULL戻り値はエラーを示しerrno、バイトを割り当てるmaxlenメモリが不足していることを示す (引数が指定されている場合NULL)、またはパスがbuffer文字よりもmaxlen長くなることをERANGE示す値に設定ENOMEMされます。 0 以下の場合maxlen、「パラメーターの検証」で説明されているように、この関数は無効なパラメーター ハンドラーを呼び出します。

これらのリターン コードとその他のリターン コードについては、「errno_doserrno_sys_errlist_sys_nerr」を参照してください。

解説

_getcwd 関数は、既定のドライブの現在の作業ディレクトリの完全なパスを取得し、それを bufferに格納します。 整数の引数 maxlen はパスの最大長を指定します。 パスの長さ (終端の null 文字を含む) が maxlenbuffer 引数は NULLになる可能性があります。パスを格納するため、 maxlen サイズ以上のバッファー (必要であればそれ以上) が mallocを使用して自動的に割り当てられます。 このバッファーは free を呼び出し、それに _getcwd の戻り値 (割り当てられるバッファーへのポインター) を渡すことにより、後で解放できます。

_getcwd は、現在の作業ディレクトリのパスを示す文字列を返します。 現在の作業ディレクトリがルートの場合、文字列は円記号 (\) で終わります。 現在の作業ディレクトリがルート以外のディレクトリの場合、文字列は、円記号ではなく、ディレクトリの名前で終わります。

ワイド文字を扱う場合は、_wgetcwd ではなく _getcwdを使用します。 buffer の場合、 _wgetcwd 引数にはワイド文字列を指定します。また戻り値もワイド文字列です。 それ以外では、_wgetcwd_getcwd の動作は同じです。

_CRTDBG_MAP_ALLOC定義されている場合_DEBUG、呼び出しは_getcwd呼び出しに_wgetcwd置き換えられ_getcwd_dbg_wgetcwd_dbg、メモリ割り当てをデバッグできます。 詳細については、「 _getcwd_dbg」と「 _wgetcwd_dbgの両方を管理できます。

既定では、この関数のグローバル状態の適用対象は、アプリケーションになります。 この動作を変更するには、「CRT のグローバル状態」を参照してください

汎用テキスト ルーチンのマップ

Tchar.h ルーチン _UNICODE_MBCS が定義されていない _MBCS が定義されている _UNICODE が定義されている
_tgetcwd _getcwd _getcwd _wgetcwd

必要条件

ルーチンによって返される値 必須ヘッダー
_getcwd <direct.h>
_wgetcwd <direct.h> または <wchar.h>

互換性の詳細については、「 Compatibility」を参照してください。

// 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

関連項目

ディレクトリ コントロール
_chdir, _wchdir
_mkdir, _wmkdir
_rmdir, _wrmdir