fnsplit
Splits a full path name into its components.
Defined in header <dir.h>
Syntax
#include <dir.h>
int fnsplit(const char *path, char *drive,char V/r,char *name, char *ext);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ |
Remarks
fnsplit 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 corre-sponding component will be parsed but not stored.)
| The maximum | sizes for these strings are given by the constants |
MAXDRIVE, MAXDIR, MAXPATH, MAXFILE, and MAXEXT (defined in dir.h), and each size includes space for the null-terminator.
| Constant | Max | String |
| MAXPATH | (80) | path |
| MAXDRIVE | (3) | drive; includes colon (:) |
| MAXDIR | (66) | dir; includes leading and trailing backslashes (\) |
| MAXFILE | (9) | name |
| MAXEXT | (5) | ext; includes leading dot (.) |
fnsplit assumes that there is enough space to store each non-null component.
When fnsplit splits path, it treats the punctuation as follows:
■ drive includes the colon (C:, A:, and so on). B dir includes the leading and trailing backslashes (\BC\include\, \source\, and so on). D name includes the file name. a ext includes the dot preceding the extension (.C, .EXE, and so on).
fnmerge and fnsplit are invertible; if you split a given path with fnsplit, then merge the resultant components with fnmerge, you end up with path.
Return value
fnsplit returns an integer (composed of five flags, defined in dir.h) indicating which of the full path name components were present in path; these flags and the components they represent are
| EXTENSION | An extension |
| FILENAME | A file name |
| DIRECTORY | A directory (and possibly subdirectories) |
| DRIVE | A drive specification (see dir.h) |
| WILDCARDS | Wildcards (* or ?) |
See also
Example
#include <stdlib.h>
#include <stdio.h>
#include <dir.h>
int main (void)
{
char *s;
char drive [MAXDRIVE];
char dir[MAXDIR];
char file[MAXFILE];
char ext[MAXEXT];
int flags;
/* get comspec environment parameter */
s = getenv ("COMSPEC");
flags = fnsplit(s,drive,dir,file, ext),•
printf( "Command processor info:\n");
if(flags & DRIVE)
printf( "\tdrive: %s\n",drive) ;
if(flags & DIRECTORY)
printf( "\tdirectory: %s\n",dir) ;
if(flags & FILENAME) -
printf ("\tfile: %s\n", file) ,•
if(flags & EXTENSION)
printf ("\textension: DS\n",ext)
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/dir.h/fnsplit/
· short form /3.1/fnsplit/