RAD Studio |  |

fnsplit, _wfnsplit

Header File

dir.h

Category

Directory Control Routines

Prototype

int fnsplit(const char *path, char *drive, char *dir, char *name, char *ext);

int _wfnsplit(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.

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 corresponding component will be parsed but not stored. If any path component is null, that component corresponds to a non-NULL, empty string.

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.

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)
- 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).

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 ?)

Example

Copy Code

    #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;
        s=getenv("COMSPEC"); /* get the comspec environment parameter */
        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: %s\n",ext);
        return 0;
    }

Portability

| POSIX | Win32 | ANSI C | ANSI C++
fnsplit |  | + |  |
_wfnsplit |  | NT only |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!