zoukankan      html  css  js  c++  java
  • 截取文件路径

    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 ", 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 ame_2

    The part to append to end is   name_3

    The appended path string is    name_1 ame_2 ame_3

  • 相关阅读:
    SynchronousQueue 的联想
    Spring Cache
    CSUOJ 1011 Counting Pixels
    CSUOJ 1973 给自己出题的小X DFS
    CSUOJ 1726 你经历过绝望吗?两次!BFS+优先队列
    CSUOJ 1900 锋芒不露
    CSUOJ 1808 地铁
    CSUOJ 1895 Apache is late again
    CSUOJ 1781 阶乘除法
    CSUOJ 1560 图书管理员的表白方式
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/10962965.html
Copyright © 2011-2022 走看看