以前也寫過分析程序運行路徑的博文http://www.BkJia.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. }
運行結果:
摘自 若有所悟