PathFindFileName

PathFindFileName(L"c:\\windows");        // windows
PathFindFileName(L"c:\\windows\\");      // windows\

PathFindFileName(L"c:");                 // c:
PathFindFileName(L"c:\\");               // c:\

 

PathFindExtension 

/*
 *    full path                      extension
 *        c:        
 *        c:\
 *        c:\windows
 *        c:\windows\
 *        c:\windows\explore
 *        c:\windows\explore.exe        .exe
 *
 *        .                             .
 *        ..                            .
 */
wcout <<PathFindExtension(L"c:\\windows\\explore.exe") <<endl;

 


 

_splitpath_s

This function comes from cstdlib, so use '_MAX_DRIVE & _MAX_DIR & _MAX_FNAME & _MAX_EXT' to define the result array size.

#include <cstdlib>
using namespace std;

/*
 *    fullpath:  c:\windows\explorer.exe   |  c:\windows\   |   c:\windows   |  c:  |  c:\  |  ..\explorer.exe
 *    drive   :   c:                       |  c:            |   c:           |  c:  |  c:   |   
 *    dir     :     \windows\              |    \windows\   |     \          |      |    \  |  ..\
 *    filename:              explorer      |                |      windows   |      |       |     explorer
 *    ext                            .exe  |                |                |      |       |             .exe
 */


const wchar_t fullpath[_MAX_PATH] = L"c:\\";

wchar_t drive[_MAX_DRIVE] = L"", dir[_MAX_DIR] = L"", 
        filename[_MAX_FNAME] = L"", ext[_MAX_EXT] = L"";

_wsplitpath_s(fullpath, 
             drive, sizeof(drive)/sizeof(drive[0]), 
             dir, sizeof(dir)/sizeof(dir[0]),
             filename, sizeof(filename)/sizeof(filename[0]), 
             ext, sizeof(ext)/sizeof(ext[0]));

 

PathAppend

/*
 *        c:\windows + explorer.exe    ->    c:\windows\explorer.exe
 *        c:\windows\ + explorer.exe   ->    c:\windows\explorer.exe
 *        c:\windows + \explorer.exe   ->    c:\windows\explorer.exe
 *
 *        c:\windows + system32        ->    c:\windows\system32
 *        c:\windows + system32\       ->    c:\windows\system32\
 */
wchar_t fullpath[MAX_PATH] = L"c:\\windows\\";
PathAppend(fullpath, L"explorer.exe\\");

 

PathRemoveFileSpec 

#include <shlwapi.h>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

#pragma comment(lib, "shlwapi.lib")

#define ArrLen(arr) (sizeof(arr)/sizeof(arr[0]))

int main(int argc, char *argv[])
{
    vector<string> v;
    v.push_back("c:\\windows\\explorer.exe");
    v.push_back("c:\\windows\\explorer");
    v.push_back("c:\\windows\\");
    v.push_back("c:\\windows");
    v.push_back("c:\\");
    v.push_back("c:");
    v.push_back("c");
    v.push_back("c:\\windows\\..\\explorer.exe");
    v.push_back(".\\explorer.exe");
    v.push_back("explorer.exe");
    v.push_back("explorer");

    for_each(v.begin(), v.end(), 
             [&v](const string &s)
             {
                 char filename[MAX_PATH] = "";
                 strcpy(filename, s.c_str());
                 ::PathRemoveFileSpec(filename);
                 cout <<setw(25) <<left <<s <<"    ->    " <<filename <<endl;
             });

    return 0;
}

fullpath & filename & extension

 


 

PathIsRelative

/*
 *    c:...        false.
 *    \....        false.
 *
 *    others       true.
 */
PathIsRelative(L"c:");            // true.

 

 

Waht is fullpath or directory? see here: http://www.cnblogs.com/walfud/articles/2634434.html

相关文章:

  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2021-07-17
  • 2021-08-27
  • 2022-01-23
猜你喜欢
  • 2021-09-15
  • 2021-07-26
  • 2022-12-23
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
相关资源
相似解决方案