Click here to Skip to main content
Licence 
First Posted 16 Feb 2000
Views 69,766
Bookmarked 40 times

CSplitPath

By Kevin Lussier | 16 Feb 2000
A class that will split a complete path into its components.
1 vote, 11.1%
1

2
1 vote, 11.1%
3
2 votes, 22.2%
4
5 votes, 55.6%
5
4.28/5 - 42 votes
μ 4.28, σa 2.39 [?]

Many times I have had the need to split a path into its components (who hasn't?). And every time I needed to do this I would use the venerable _splitpath() command. I often wondered why there isn't a class wrapper for this function. So here it is, CSplitPath. It's pretty much self-explanatory: Create an instance of the class with the path to split in the constructor. Then use the member functions to get the pieces you want.

It's that simple. Enjoy!


// Example usage
CSplitPath sp( "C:\\Temp\\Foo.txt" );
cout << "Drive: " << sp.GetDrive()
cout << "Directory: " << sp.GetDirectory();
cout << "File Name: " << sp.GetFileName();
cout << "Extension: " << sp.GetExtension();

The source code is very small and is as follows:

//////////////////////////////////////////////////////////////////////
// SplitPath.h : interface for the CSplitPath class.                //
// (c) 1999, Kevin Lussier                                          //
//////////////////////////////////////////////////////////////////////

#ifndef __SPLITPATH_H__
#define __SPLITPATH_H__

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

class CSplitPath
{ 
// Construction
public: 
    CSplitPath( LPCTSTR lpszPath = NULL );

// Operations
    BOOL    Split(LPCTSTR lpszPath );
    CString GetPath( void ) { return path_buffer; }
    CString GetDrive( void ) { return drive; }
    CString GetDirectory( void ) { return dir; }
    CString GetFileName( void ) { return fname; }
    CString GetExtension( void ) { return ext; }

// Attributes
protected:
    TCHAR path_buffer[_MAX_PATH];
    TCHAR drive[_MAX_DRIVE];
    TCHAR dir[_MAX_DIR];
    TCHAR fname[_MAX_FNAME];
    TCHAR ext[_MAX_EXT];
}; 

#endif  // __SPLITPATH_H__

//////////////////////////////////////////////////////////////////////
// SplitPath.cpp : implementation of the CSplitPath class.          //
// (c) 1999, Kevin Lussier                                          //
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SplitPath.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CSplitPath::CSplitPath( LPCTSTR lpszPath )
{
    // Initialize our objects
    memset( path_buffer, 0, sizeof( path_buffer ) );
    memset( drive, 0, sizeof( drive ) );
    memset( dir, 0, sizeof( dir ) );
    memset( fname, 0, sizeof( fname ) );
    memset( ext, 0, sizeof( ext ) );

    // If we were given a path, split it
    if ( lpszPath ) {
        Split( lpszPath );
    }
}

BOOL CSplitPath::Split( LPCTSTR lpszPath )
{
    // If we weren't given a path, fail
    if ( lpszPath == NULL ) {
        // Return failure
        return FALSE;
    }

    // Copy the path
    _tcsncpy( path_buffer, lpszPath, sizeof( path_buffer ) - 1 );
    // Split the given path
    _tsplitpath( path_buffer, drive, dir, fname, ext );

    return TRUE;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kevin Lussier



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat about relative paths? Pinmembersoundman3223:20 19 Dec '04  
QuestionWhy is Split(LPCTSTR lpszPath) a public member? PinmemberKevin Cotter4:48 10 Jul '03  
AnswerRe: Why is Split(LPCTSTR lpszPath) a public member? PinmemberDavid Pritchard5:56 28 Sep '07  
Generalpotential problem PinsussAnonymous13:34 5 Jun '03  
QuestionEasier ? PinsussKochise0:23 20 Sep '02  
GeneralSweet! Pinmembercaleb4:01 14 Mar '02  
GeneralEnhancement to cope with UNC path PinsussGeert Delmeiren3:52 18 Feb '00  
GeneralRe: Enhancement to cope with UNC path PinmemberGennady Oster2:42 21 Jun '01  
GeneralGood point PinmemberGeert Delmeiren21:46 15 Jul '01  
GeneralVery Useful ! PinsussSteven J. Ackerman8:03 17 Feb '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120209.1 | Last Updated 17 Feb 2000
Article Copyright 2000 by Kevin Lussier
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid