Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5955555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:07:51+00:00 2026-05-22T18:07:51+00:00

Question How do you extract the base path from pathname in C? Are there

  • 0

Question

How do you extract the base path from pathname in C?

Are there any functions built into the C language or the C-Runtime Library to extract the base path from a pathname in C?

I’m asking basically the opposite of this question.

NOTE: I prefer a cross-platform solution, but I’m working in Windows so if there is a Windows API call that does this, I’d still like to know.

Examples

Input              | Output
---------------------------------
C:\path\to\file   -> C:\path\to\
C:\path\to\file.c -> C:\path\to\
C:\file           -> C:\
.\file            -> .\
.\                -> .\
\                 -> \

References

  • Extract file name from full path in C using MSVS2005
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:07:51+00:00Added an answer on May 22, 2026 at 6:07 pm

    On Windows there is _splitpath.

    Example

    #include <Windows.h>
    #include <tchar.h>
    
    // Use your own error codes here
    #define SUCCESS                     0L
    #define FAILURE_NULL_ARGUMENT       1L
    #define FAILURE_API_CALL            2L
    #define FAILURE_INSUFFICIENT_BUFFER 3L
    
    DWORD GetBasePathFromPathName( LPCTSTR szPathName,
                                   LPTSTR  szBasePath,
                                   DWORD   dwBasePathSize )
    {
      TCHAR   szDrive[_MAX_DRIVE] = { 0 };
      TCHAR   szDir[_MAX_DIR]     = { 0 };
      TCHAR   szFname[_MAX_FNAME] = { 0 };
      TCHAR   szExt[_MAX_EXT]     = { 0 };
      size_t  PathLength;
      DWORD   dwReturnCode;
    
      // Parameter validation
      if( szPathName == NULL || szBasePath == NULL )
      {
        return FAILURE_NULL_ARGUMENT;
      }
    
      // Split the path into it's components
      dwReturnCode = _tsplitpath_s( szPathName, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFname, _MAX_FNAME, szExt, _MAX_EXT );
      if( dwReturnCode != 0 )
      {
        _ftprintf( stderr, TEXT("Error splitting path. _tsplitpath_s returned %d.\n"), dwReturnCode );
        return FAILURE_API_CALL;
      }
    
      // Check that the provided buffer is large enough to store the results and a terminal null character
      PathLength = _tcslen( szDrive ) + _tcslen( szDir );
      if( ( PathLength + sizeof( TCHAR ) ) > dwBasePathSize )
      {
        _ftprintf( stderr, TEXT("Insufficient buffer. Required %d. Provided: %d\n"), PathLength, dwBasePathSize );
        return FAILURE_INSUFFICIENT_BUFFER;
      }
    
      // Copy the szDrive and szDir into the provide buffer to form the basepath
      if( ( dwReturnCode = _tcscpy_s( szBasePath, dwBasePathSize, szDrive ) ) != 0 )
      {
        _ftprintf( stderr, TEXT("Error copying string. _tcscpy_s returned %d\n"), dwReturnCode );
        return FAILURE_API_CALL;
      }
      if( ( dwReturnCode = _tcscat_s( szBasePath, dwBasePathSize, szDir ) ) != 0 )
      {
        _ftprintf( stderr, TEXT("Error copying string. _tcscat_s returned %d\n"), dwReturnCode );
        return FAILURE_API_CALL;
      }
      return SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running the below query to extract data from an xml column...question is
Continuing from my other question , I now know how to extract the fieldnames
similar to this question: preg_match to extract mailto on anchor but I am trying
My question follows on from another stackoverflow question:- How to get the root node
I'm working with a C++ code base. Right now I'm using a C++ code
GENERAL XML OUTLINE: <dasbhoards> <dashboard name=S1> <repository-location derived-from='http://dataviz.win.compete.com/workbooks/OTCSurvey_06_15_11_16_54/RT4?rev=' id='RT4' path='/workbooks/RetailFootwear' revision='' /> <style> </style>
Question Updated I am building an MVC framework, for my templates and views, I
I have to extract only this url out of a list 3-4 urls Url
I've seen some discussions in SO and other places regarding how this should be
I have 4 tables in my SQLite database. One with questions, one with answers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.