以前也写过分析程序运行路径的博文http://blog.csdn.net/qiuyeting/article/details/7305077,但今天偶然发现有更好的函数可以帮助我们分析路径参数。这个函数就是_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.
#include <stdlib.h> #include <stdio.h> #include <string.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]; //获取当前运行程序的完整路径,赋值给path_buffer strcpy(path_buffer,__argv[0]); _splitpath( path_buffer, drive, dir, fname, ext ); // Note: _splitpath is deprecated; consider using _splitpath_s instead 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 ); return 0; }
运行结果: