【MoreWindows工作笔记6】PathCommonPrefix 路径公共前缀

工作后比较忙,没大块的时间来酝酿博客的写作,所以随时记点笔记,一来方便自己查阅,二来也督促自己学习和总结。如果能对大家有所帮助,就更加开心了大笑


提取出路径的公共前缀是个比较实用的功能,函数原型如下:

int PathCommonPrefix(      
    LPCTSTR pszFile1,
    LPCTSTR pszFile2,
    LPTSTR pszPath
);

各个参数解释如下(这英语相当简单~):

pszFile1
[in] A pointer to a null-terminated string of maximum length MAX_PATH that contains the first path name.
pszFile2
[in] A pointer to a null-terminated string of maximum length MAX_PATH that contains the second path name.
pszPath
[out] A pointer to a buffer that receives the common prefix. This buffer is assumed to be at least MAX_PATH characters in size. If there is no common prefix, it will be set to NULL.

下面给出示范:

// 【MoreWindows工作笔记6】PathCommonPrefix 路径的公共前缀
//  http://blog.csdn.net/morewindows/article/details/17078619
#include <windows.h>
#include <Shlwapi.h>
#include <string>
#include <iostream>
using namespace std;
#pragma comment(lib, "shlwapi.lib")
int main()
{
  printf("   【MoreWindows工作笔记6】PathCommonPrefix 路径的公共前缀\n");    
  printf(" - http://blog.csdn.net/morewindows/article/details/17078619 -\n");    
  printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");

  // 路径公共前缀
  string src_path = "C:\\test1\\test"; //"\\home\\test1\\test";
  string des_path = "C:\\test1\\test2"; //"\\home\\test1\\test2";
  char prefix_path[MAX_PATH];
  PathCommonPrefix(src_path.c_str(), des_path.c_str(), prefix_path);

  cout<<"源路径:   "<<src_path<<endl;
  cout<<"目标路径: "<<des_path<<endl;
  cout<<"-----------------------------------"<<endl;
  cout<<"路径共有的前缀为: "<<prefix_path<<endl;
  if (prefix_path == src_path)
    cout<<" 目标路径包含源路径"<<endl;
  else
    cout<<" 目标路径不包含源路径"<<endl;
  cout<<"-----------------------------------"<<endl;
  return 0;
}
运行结果如图所示:

【MoreWindows工作笔记6】PathCommonPrefix 路径公共前缀_第1张图片


本篇《【MoreWindows工作笔记6】PathCommonPrefix 路径的公共前缀》介绍了使用函数提取路径的公共前缀,并使用公共前缀来判断路径的包含关系,下一篇《【MoreWindows工作笔记7】PathIsPrefix 判断路径的包含关系》将介绍一个更简单的API函数来判断路径的包含关系——PathIsPrefix。欢迎继续浏览~


地址:http://blog.csdn.net/morewindows/article/details/17078619  转载请标明出处,谢谢。

欢迎关注微博:http://weibo.com/MoreWindows  




你可能感兴趣的:(windows,路径操作API)