_makepath 和 _splitpath

Create a path name from components.
 
void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext );
 
需要的头文件<stdlib.h>
无返回值
 
Parameters
path
Full path buffer
drive
Drive letter
dir
Directory path
fname
Filename
ext
File extension
 
Remarks
The _makepath function creates a single path and stores it in path. The path may include a drive letter, directory path, filename, and filename extension.
 
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 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 filename. The trailing slash is optional, and either a forward slash (/) or a backslash (\) or both may 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 filename without any extensions. If fname is NULL or points to an empty string, no filename is inserted in the composite path string.
ext
Contains the actual filename extension, with or without a leading period (.). _makepath 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 may be larger than the current operating-system version will handle.
 
Example
#include <stdlib.h> 
#include <stdio.h> 
void main( void ) 

 char path_buffer[_MAX_PATH]; 
 char drive[_MAX_DRIVE]; 
 char dir[_MAX_DIR]; 
 char fname[_MAX_FNAME]; 
 char ext[_MAX_EXT]; 


 _makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );     //路径的合并
 printf( "Path created with _makepath: %s\n\n", path_buffer ); 
 _splitpath( path_buffer, drive, dir, fname, ext );         //路径的分解
 printf( "Path extracted with _splitpath:\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: c:\sample\crt\makepath.c 


Path extracted with _splitpath: 
 Drive: c: 
 Dir: \sample\crt\ 
 Filename: makepath 
 Ext: .c  




转自:http://blog.sina.com.cn/s/blog_4087da210100mrxy.html
`_splitpath` 是一个 C/C++ 标准库函数,用于将文件路径分解成文件名、目录路径和文件扩展名等部分。它的原型定义如下: ```c++ void _splitpath( const char * path, char * drive, char * dir, char * fname, char * ext ); ``` 参数说明: - `path`:要分解的文件路径。 - `drive`:指向存储驱动器号的缓冲区的指针。如果路径中没有驱动器号,则此参数返回空字符串。 - `dir`:指向存储目录路径的缓冲区的指针。如果路径中没有目录,则此参数返回空字符串。 - `fname`:指向存储文件名的缓冲区的指针。如果路径中没有文件名,则此参数返回空字符串。 - `ext`:指向存储文件扩展名的缓冲区的指针。如果路径中没有扩展名,则此参数返回空字符串。 `_splitpath` 函数分解路径的方式与操作系统有关,Windows 系统下的路径分解方式如下: - 路径以驱动器号开头,则将驱动器号分配给 `drive` 参数,`dir` 参数包含路径中的所有目录,但不包括驱动器号。 - 路径不以驱动器号开头,但以斜杠或反斜杠字符开头,则将根目录分配给 `dir` 参数,`drive` 参数为空字符串。 - 如果路径中有文件名,则 `_splitpath` 函数将文件名分配给 `fname` 参数,如果还有扩展名,则将扩展名分配给 `ext` 参数。 下面是一个使用 `_splitpath` 函数的示例: ```c++ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> void print_path_parts(const char* path) { char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath(path, drive, dir, fname, ext); printf("Drive: %s\n", drive); printf("Dir: %s\n", dir); printf("Filename: %s\n", fname); printf("Extension: %s\n", ext); } int main() { const char* path = "C:\\Program Files\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64\\cl.exe"; print_path_parts(path); return 0; } ``` 输出结果如下: ``` Drive: C: Dir: \Program Files\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\ Filename: cl Extension: .exe ``` 可以看到,该程序将路径分解成了驱动器号、目录路径、文件名和文件扩展名四部分,分别存储在 `drive`、`dir`、`fname` 和 `ext` 变量中,并打印输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值