_makepath 和 _splitpath

本文介绍如何使用_makepath函数从各个组成部分创建完整的路径名。该函数可以接收驱动器字母、目录路径、文件名和扩展名等参数,并将它们组合成一个完整的路径字符串。文章还提供了示例代码来演示函数的用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值