截取文件路径

BOOL PathRemoveFileSpec( LPTSTR pszPath);

功能:删除路径后面的文件名和’/’符号。该函数可以分析出一个文件的路径。

 

例:char szpath[MAX_PATH]=”d://test//111.txt”;

调用PathRemoveFileSpec( szpath ) 后,szPath = “d://test”

 

BOOL PathAppendA(

  LPSTR  pszPath,

  LPCSTR pszMore

);

功能:动态添加搜索路径设置

 

头文件为

#include <Shlwapi.h>

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

 

 

例如,我们想获取EXE文件自身所在的文件夹,可以这样:

 

    #include <stdio.h>

    #include <Shlwapi.h>

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

    

    int main() 

    { 

          TCHAR szPath[MAX_PATH];

          //获取应用程序或者DLL的完整路径

          ::GetModuleFileName(NULL, szPath, MAX_PATH);

          //去掉路径末尾的文件名和反斜杠

          ::PathRemoveFileSpec(szPath);

    

          printf("%ls\n", szPath);

    

          return 0;

    }

 

例如:

#include <windows.h>

#include <iostream>

#include "Shlwapi.h"

 

using namespace std;

 

int main( void )

{

      // String for path name.

      char buffer_1[MAX_PATH] = "name_1\\name_2";

      char *lpStr1;

      lpStr1 = buffer_1;

 

      // String of what is being added.

      char buffer_2[ ] = "name_3";

      char *lpStr2;

      lpStr2 = buffer_2;

 

      cout << "The original path string is    " << lpStr1 << endl;

      cout << "The part to append to end is   " << lpStr2 << endl;

      bool ret = PathAppend(lpStr1,lpStr2);

      cout << "The appended path string is    " << lpStr1 << endl;

}

 

OUTPUT:

---------

The original path string is    name_1\name_2

The part to append to end is   name_3

The appended path string is    name_1\name_2\name_3

 

posted @ 2019-06-02 14:53  gd_沐辰  阅读(812)  评论(0编辑  收藏  举报