码迷,mamicode.com
首页 > 其他好文 > 详细

去除路径中的后缀名和获取路径目录

时间:2017-12-08 14:06:59      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:comm   有用   去除   using   clipboard   bsp   cpp   cli   源代码   

首先,记录一个网址,感觉很有用,大部分的文件路径相关函数,里面都有源代码。

https://msdn.microsoft.com/en-us/library/windows/desktop/bb773746(v=vs.85).aspx 

1、完整路径,去除后缀名   PathRemoveExtensionA

 

[cpp] view plain copy
 
  1. #include <iostream>//cout函数所需  
  2. #include "atlstr.h"  //PathRemoveExtensionA函数所需  
  3.   
  4. using namespace std;  
  5.   
  6. void main(void)  
  7. {  
  8.     char buffer_1[] = "C:\\TEST\\sample.txt";  
  9.     char *lpStr1;  
  10.     lpStr1 = buffer_1;  
  11.     cout << "The path with extension is          : " << lpStr1 << endl;  
  12.     PathRemoveExtensionA(lpStr1);  
  13.     cout << "\nThe path without extension is       : " << lpStr1 << endl;  
  14.     system("pause");  
  15. }  
OUTPUT:
==================
The path with extension is          : C:\TEST\sample.txt
The path without extension is       : C:\TEST\sample

 

2、完整文件路径,获得目录

 

[cpp] view plain copy
 
  1. #include <iostream>//cout函数所需  
  2. #include "atlstr.h"  //PathRemoveFileSpecA函数所需  
  3.   
  4. using namespace std;  
  5.   
  6. void main(void)  
  7. {  
  8.     char buffer_1[] = "C:\\TEST\\sample.txt";  
  9.     char *lpStr1;  
  10.     lpStr1 = buffer_1;  
  11.     cout << "The path with file spec is          : " << lpStr1 << endl;  
  12.     PathRemoveFileSpecA(lpStr1);  
  13.     cout << "\nThe path without file spec is       : " << lpStr1 << endl;  
  14.     //注意如果获得了目录,需要得到另一个文件路径时  
  15.     string filename = lpStr1;  
  16.     filename = filename + "\\samle.txt";  
  17.     system("pause");  
  18. }  

 

OUTPUT:
==================
The path with file spec is          : C:\TEST\sample.txt
The path without file spec is       : C:\TEST

3、获取dll所在路径的两种方式

(1)需要dll入口函数的句柄

 

[cpp] view plain copy
 
  1. char szPath[MAX_PATH];  
  2. GetModuleFileNameA(dllhandle, szPath, MAX_PATH);//BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved) //dll入口函数  

 

(2)无需dll入口函数的句柄,dll内任意函数都可

 

[cpp] view plain copy
 
  1. EXTERN_C IMAGE_DOS_HEADER __ImageBase;//申明为全局变量  
  2. char   DllPath[MAX_PATH] = { 0 };  
  3. GetModuleFileNameA((HINSTANCE)&__ImageBase, DllPath, _countof(DllPath))

去除路径中的后缀名和获取路径目录

标签:comm   有用   去除   using   clipboard   bsp   cpp   cli   源代码   

原文地址:http://www.cnblogs.com/wangjian8888/p/8004491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!