_makepath_s, _wmakepath_s

從元件建立路徑名稱。 這些函式是 的版本 _makepath_wmakepath 具有 CRT 中安全性功能中所述 的安全性增強功能。

語法

errno_t _makepath_s(
   char *path,
   size_t sizeInBytes,
   const char *drive,
   const char *dir,
   const char *fname,
   const char *ext
);
errno_t _wmakepath_s(
   wchar_t *path,
   size_t sizeInWords,
   const wchar_t *drive,
   const wchar_t *dir,
   const wchar_t *fname,
   const wchar_t *ext
);
template <size_t size>
errno_t _makepath_s(
   char (&path)[size],
   const char *drive,
   const char *dir,
   const char *fname,
   const char *ext
); // C++ only
template <size_t size>
errno_t _wmakepath_s(
   wchar_t (&path)[size],
   const wchar_t *drive,
   const wchar_t *dir,
   const wchar_t *fname,
   const wchar_t *ext
); // C++ only

參數

path
完整路徑緩衝區。

sizeInWords
以字組為單位的緩衝區大小。

sizeInBytes
以位元組為單位的 緩衝區大小。

drive
包含對應至所需磁碟機的代號 (A、B 等) 及選擇性後置冒號。 _makepath_s 如果遺漏冒號,就會自動在複合路徑中插入冒號。 如果 driveNULL 或指向空字串,複合 path 字串中就不會出現磁碟機代號。

dir
包含目錄路徑,但不包含磁碟機指示項或實際檔案名稱。 尾端斜線是選擇性的,而且正斜線 (/) 或反斜線 (\) 或兩者都可用於單 dir 一引數。 如果未指定尾端斜線 (/ 或 \) ,則會自動插入。 如果 dirNULL 或指向空字串,複合 path 字串中就不會插入目錄路徑。

fname
包含基底檔案名稱,但不包含任何副檔名。 如果 fnameNULL 或指向空字串,複合 path 字串中就不會插入檔名。

ext
包含實際副檔名,可包含或不含前置句號 (.)。 _makepath_s 如果期間未出現在 中 ext ,則會自動插入句點。 如果 extNULL 或指向空字串,複合 path 字串中就不會插入副檔名。

傳回值

如果成功,就是零,如果失敗,則為錯誤碼。

錯誤條件

path sizeInWords / sizeInBytes 傳回 path 的內容。
NULL 任意 EINVAL 未修改
任意 <= 0 EINVAL 未修改

如果發生上述任何錯誤狀況,這些函式會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,errno 會設定為 EINVAL,且函式會傳回 EINVAL。 參數 drivefnameext 可使用 NULL。 如需這些參數為 null 指標或空字串時之行為的資訊,請參閱<備註>一節。

備註

_makepath_s 函式會從個別元件建立複合路徑字串,並將結果儲存在 path 中。 path 可能包含磁碟機代號、目錄路徑、檔案名稱和副檔名。 _wmakepath_s_makepath_s的寬字元版本; _wmakepath_s 的引數是寬字元字串。 否則,_wmakepath_s_makepath_s 的行為即會相同。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

Tchar.h 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tmakepath_s _makepath_s _makepath_s _wmakepath_s

path 引數必須指向大小足以容納完整路徑的空白緩衝區。 複合 path 不得大於 _MAX_PATH 常數 (定義於 Stdlib.h 中)。

如果 path 為 NULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 此外,errno 會設定為 EINVAL。 所有其他參數都可使用 NULL 值。

C++ 利用多載樣板簡化了這些函式的使用方式。多載可自動推斷緩衝區長度 (因而不須指定大小引數),也可以將不安全的舊函式自動取代成較新且安全的對應函式。 如需詳細資訊,請參閱 保護範本多載

這些函式的偵錯程式庫版本會先將緩衝區填入0xFE。 若要停用此行為,請使用 _CrtSetDebugFillThreshold

需求

常式 必要的標頭
_makepath_s <stdlib.h>
_wmakepath_s <stdlib.h > 或 < wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_makepath_s.c

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

int main( void )
{
   char path_buffer[_MAX_PATH];
   char drive[_MAX_DRIVE];
   char dir[_MAX_DIR];
   char fname[_MAX_FNAME];
   char ext[_MAX_EXT];
   errno_t err;

   err = _makepath_s( path_buffer, _MAX_PATH, "c", "\\sample\\crt\\",
                      "crt_makepath_s", "c" );
   if (err != 0)
   {
      printf("Error creating path. Error code %d.\n", err);
      exit(1);
   }
   printf( "Path created with _makepath_s: %s\n\n", path_buffer );
   err = _splitpath_s( path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname,
                       _MAX_FNAME, ext, _MAX_EXT );
   if (err != 0)
   {
      printf("Error splitting the path. Error code %d.\n", err);
      exit(1);
   }
   printf( "Path extracted with _splitpath_s:\n" );
   printf( "   Drive: %s\n", drive );
   printf( "   Dir: %s\n", dir );
   printf( "   Filename: %s\n", fname );
   printf( "   Ext: %s\n", ext );
}
Path created with _makepath_s: c:\sample\crt\crt_makepath_s.c

Path extracted with _splitpath_s:
   Drive: c:
   Dir: \sample\crt\
   Filename: crt_makepath_s
   Ext: .c

另請參閱

檔案處理
_fullpath, _wfullpath
_splitpath_s, _wsplitpath_s
_makepath, _wmakepath