_splitpath、_wsplitpath_splitpath, _wsplitpath

将路径名称分解成组件。Break a path name into components. 提供这些函数的更多安全版本;请参阅 _splitpath_s、_wsplitpath_sMore secure versions of these functions are available, see _splitpath_s, _wsplitpath_s.

语法Syntax

void _splitpath(
   const char *path,
   char *drive,
   char *dir,
   char *fname,
   char *ext
);
void _wsplitpath(
   const wchar_t *path,
   wchar_t *drive,
   wchar_t *dir,
   wchar_t *fname,
   wchar_t *ext
);

参数Parameters

pathpath
完整路径。Full path.

驱动器drive
驱动器号后, 接一个冒号 (:)。Drive letter, followed by a colon (:). 可以将传递NULL为此参数,如果不需要驱动器号。You can pass NULL for this parameter if you do not need the drive letter.

dirdir
目录路径,包括尾部反斜杠。Directory path, including trailing slash. 正斜杠 ( / ),反斜杠 ( \ ),或两者均使用。Forward slashes ( / ), backslashes ( \ ), or both may be used. 可以将传递NULL为此参数,如果不需要的目录路径。You can pass NULL for this parameter if you do not need the directory path.

fnamefname
基文件名(无扩展名)。Base filename (no extension). 可以将传递NULL为此参数,如果不需要文件名。You can pass NULL for this parameter if you do not need the filename.

extext
文件扩展名,包括前导句点 ()。Filename extension, including leading period (.). 可以将传递NULL为此参数,如果不需要文件扩展名。You can pass NULL for this parameter if you do not need the filename extension.

备注Remarks

_Splitpath函数将路径分解成四个组件。The _splitpath function breaks a path into its four components. _splitpath自动处理多字节字符字符串参数,根据需要,根据当前正在使用的多字节代码页识别多字节字符序列。_splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath是宽字符版本 _splitpath; 的自变量 _wsplitpath都是宽字符字符串。_wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. 否则这些函数具有相同行为。These functions behave identically otherwise.

安全说明这些函数会引发由缓冲区溢出问题带来的潜在威胁。Security Note These functions incur a potential threat brought about by a buffer overrun problem. 缓冲区溢出问题是常见的系统攻击方法,使权限的提升不能确保。Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. 有关详细信息,请参阅 避免缓冲区溢出For more information, see Avoiding Buffer Overruns. 提供这些函数的更多安全版本;请参阅 _splitpath_s、_wsplitpath_sMore secure versions of these functions are available; see _splitpath_s, _wsplitpath_s.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tsplitpath_tsplitpath _splitpath_splitpath _splitpath_splitpath _wsplitpath_wsplitpath

每个组件的完整路径存储在单独的缓冲区中;清单常量 _MAX_DRIVE_MAX_DIR_MAX_FNAME,以及 _MAX_EXT (STDLIB 中定义。H) 指定每个文件组件的最大大小。Each component of the full path is stored in a separate buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size for each file component. 文件组件大于相应清单常量会导致堆损坏。File components that are larger than the corresponding manifest constants cause heap corruption.

每个缓冲区必须与其相应的清单常量一样大,以避免潜在的缓冲区溢出。Each buffer must be as large as its corresponding manifest constant to avoid potential buffer overrun.

下表列出了清单常量的值。The following table lists the values of the manifest constants.

nameName “值”Value
_MAX_DRIVE_MAX_DRIVE 33
_MAX_DIR_MAX_DIR 256256
_MAX_FNAME_MAX_FNAME 256256
_MAX_EXT_MAX_EXT 256256

如果完整路径不包含组件 (例如,文件名), _splitpath空字符串分配给相应的缓冲区。If the full path does not contain a component (for example, a filename), _splitpath assigns empty strings to the corresponding buffers.

可以将传递NULL_splitpath以外的其他任何参数路径不需要的。You can pass NULL to _splitpath for any parameter other than path that you do not need.

如果路径NULL,将调用无效参数处理程序,如中所述参数验证If path is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则errno设置为EINVAL并且该函数返回EINVALIf execution is allowed to continue, errno is set to EINVAL and the function returns EINVAL.

要求Requirements

例程所返回的值Routine 必需的标头Required header
_splitpath_splitpath <stdlib.h><stdlib.h>
_wsplitpath_wsplitpath <stdlib.h> 或 <wchar.h><stdlib.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

请参阅 _makepath 的示例。See the example for _makepath.

请参阅See also

文件处理File Handling
_fullpath、_wfullpath_fullpath, _wfullpath
_getmbcp_getmbcp
_makepath、_wmakepath_makepath, _wmakepath
_setmbcp_setmbcp
_splitpath_s、_wsplitpath_s_splitpath_s, _wsplitpath_s