_getcwd、_wgetcwd

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

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

パラメーター

  • buffer
    パスの格納場所。

  • maxlen
    文字のパスの最大長:charfor _getcwd and wchar_t for _wgetcwd.

戻り値

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

リターン コードの詳細については、「_doserrno、errno、_sys_errlist、および _sys_nerr」を参照してください。

解説

_getcwd 関数は、既定のドライブの現在の作業ディレクトリを完全パスで取得し、buffer に格納します。 整数の引数である maxlen には、パスの最大長を指定します。 末尾の NULL 文字を含むパスの長さが maxlen を超える場合、エラーが発生します。buffer 引数には NULL を指定できます。この場合、malloc を使用すると、少なくとも maxlen のバッファー (必要な場合のみ拡張) がパスを格納するために自動的に割り当てられます。 このバッファーは、free を呼び出し、_getcwd の戻り値 (割り当てられたバッファーへのポインター) を渡すことによって、後で解放できます。

_getcwd は、現在の作業ディレクトリのパスを表す文字列を返します。 現在の作業ディレクトリがルートの場合、文字列の末尾には円記号 (\) が付きます。 現在の作業ディレクトリがルート以外のディレクトリの場合、文字列はディレクトリ名で終わり、末尾に円記号は付きません。

_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> または <wchar.h>

互換性の詳細については、「C ランタイム ライブラリ」の「互換性」を参照してください。

使用例

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