分析路径参数函数_splitpath
以前也写过分析程序运行路径的博文http://www.zzzyk.com/kf/201203/125323.html ,但今天偶然发现有更好的函数可以帮助我们分析路径参数。这个函数就是_splitpath。
函数原型:
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
);
参数含义:
path
Full path buffer. _makepath does not check that path does not exceed _MAX_PATH.
drive
Drive letter.
dir
Directory path.
fname
File name.
ext
File name extension.
实例:
[cpp]
1. #include <stdlib.h>
2. #include <stdio.h>
3. #include <string.h>
4. int main( void )
5. {
6. char path_buffer[_MAX_PATH];
7. char drive[_MAX_DRIVE];
8. char dir[_MAX_DIR];
9. char fname[_MAX_FNAME];
10. char ext[_MAX_EXT];
11.
12. //获取当前运行程序的完整路径,赋值给path_buffer
13. strcpy(path_buffer,__argv[0]);
14. _splitpath( path_buffer, drive, dir, fname, ext );
15. // Note: _splitpath is deprecated; consider using _splitpath_s instead
16. printf( "Path extracted with _splitpath:\n" );
17. printf( " Drive: %s\n", drive );
18. printf( " Dir: %s\n", dir );
19. printf( " Filename: %s\n", fname );
20. printf( " Ext: %s\n", ext );
21.
22. return 0;
23. }
运行结果:
摘自 若有所悟
补充:软件开发 , C语言 ,