_makepath_s, _wmakepath_s
Create a path name from components. These are versions of _makepath, _wmakepath with security enhancements as described in Security Enhancements in the CRT.
errno_t _makepath_s(
char *path,
size_t sizeInWords,
const char *drive,
const char *dir,
const char *fname,
const char *ext
);
errno_t _wmakepath_s(
wchar_t *path,
size_t sizeInBytes,
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
Параметры
[out] path
Full path buffer.[in] sizeInWords
Size of the buffer in words.[in] sizeInBytes
Size of the buffer in bytes.[in] drive
Drive letter.[in] dir
Directory path.[in] fname
File name.[in] ext
File name extension.
Возвращаемое значение
Zero if successful; an error code on failure.
Error Conditions
path |
sizeInWords / sizeInBytes |
Return |
Contents of path |
---|---|---|---|
NULL |
any |
EINVAL |
not modified |
any |
<= 0 |
EINVAL |
not modified |
If any of the above error conditions occurs, these functions invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, errno is set toEINVAL and the functions returnsEINVAL**.** NULL is allowed for the parameters drive, fname, and ext. For information about the behavior when these parameters are null pointers or empty strings, see the Remarks section.
Заметки
The _makepath_s function creates a single path and stores it in path. The path might include a drive letter, directory path, file name, and file name extension. _wmakepath_s is a wide-character version of _makepath_s; the arguments to _wmakepath_s are wide-character strings. _wmakepath_s and _makepath_s behave identically otherwise.
Generic-Text Routine Mappings
Tchar.h routine |
_UNICODE and _MBCS not defined |
_MBCS defined |
_UNICODE defined |
---|---|---|---|
_tmakepath_s |
_makepath_s |
_makepath_s |
_wmakepath_s |
The following arguments point to buffers containing the path elements.
drive
Contains a letter (A, B, and so on) corresponding to the desired drive and an optional trailing colon. _makepath_s inserts the colon automatically in the composite path if it is missing. If drive is a null character or an empty string, no drive letter and colon appear in the composite path string.dir
Contains the path of directories, not including the drive designator or the actual file name. The trailing slash is optional, and either a forward slash (/) or a backslash (\) or both might be used in a single dir argument. If a trailing slash (/ or \) is not specified, it is inserted automatically. If dir is a null character or an empty string, no slash is inserted in the composite path string.fname
Contains the base file name without any extensions. If fname is NULL or points to an empty string, no file name is inserted in the composite path string.ext
Contains the actual file name extension, with or without a leading period (.). _makepath_s inserts the period automatically if it does not appear in ext. If ext is a null character or an empty string, no period is inserted in the composite path string.
The path argument must point to an empty buffer large enough to hold the complete path. Although there are no size limits on any of the fields that constitute path, the composite path must be no larger than the _MAX_PATH constant, defined in Stdlib.h. _MAX_PATH might be larger than the current operating-system version can handle.
In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.
The debug versions of these functions first fill the buffer with 0xFD. To disable this behavior, use _CrtSetDebugFillThreshold.
Требования
Routine |
Required header |
---|---|
_makepath_s |
<stdlib.h> |
_wmakepath_s |
<stdlib.h> or <wchar.h> |
For more compatibility information, see Compatibility in the Introduction.
Пример
// 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 );
}
Output
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