_splitpath, _wsplitpath
Go Up to stdlib.h Index
Header File
stdlib.h
Category
Directory Control Routines
Prototype
void _splitpath(const char *path, char *drive, char *dir, char *name, char *ext);
void _wsplitpath(const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *name, wchar_t *ext);
Description
Splits a full path name into its components.
_splitpath takes a file's full path name (path) as a string in the form
X:\DIR\SUBDIR\NAME.EXT
and splits path into its four components. It then stores those components in the strings pointed to by drive, dir, name, and ext. (All five components must be passed, but any of them can be a null, which means the corresponding component will be parsed but not stored.) The maximum sizes for these strings are given by the constants _MAX_DRIVE, _MAX_DIR, _MAX_PATH, _MAX_FNAME, and _MAX_EXT (defined in stdlib.h), and each size includes space for the null-terminator. These constants are defined in stdlib.h.
_MAX_PATH |
path |
_MAX_DRIVE |
drive; includes colon (:) |
_MAX_DIR |
dir; includes leading and trailing backslashes (\) |
_MAX_FNAME |
name |
_MAX_EXT |
ext; includes leading dot (.) |
_splitpath assumes that there is enough space to store each non-null component.
When _splitpath splits path, it treats the punctuation as follows:
- drive includes the colon (C:, A:, and so on).
- dir includes the leading and trailing backslashes (\BC\include\, \source\, and so on).
- name includes the file name.
- ext includes the dot preceding the extension (.C, .EXE, and so on).
_makepath and _splitpath are invertible; if you split a given path with _splitpath, then merge the resultant components with _makepath, you end up with path.
Return Value
None.
Example
#include <dir.h> #include <string.h> #include <stdio.h> #include <stdlib.h> int main(void) { char s[_MAX_PATH]; char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char file[_MAX_FNAME]; char ext[_MAX_EXT]; /* get current working directory */ getcwd(s,_MAX_PATH); if (s[strlen(s)-1] != '\\') /* append a trailing \ character */ strcat(s,"\\"); /* split the string to separate elems */ _splitpath(s,drive,dir,file,ext); strcpy(file,"DATA"); strcpy(ext,".TXT"); /* merge everything into one string */ _makepath(s,drive,dir,file,ext); /* display resulting string */ puts(s); return 0; }
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
_splitpath |
+ |
|||
_wsplitpath |
+ |