c/c++ 获取当前程序(EXE)所在的路径

来源:互联网 发布:人工智能行业研究报告 编辑:程序博客网 时间:2024/05/22 06:38

一、

1.只获得路径字串不包含文件名

TCHAR szFilePath[MAX_PATH + 1]={0};
GetModuleFileName(NULL, szFilePath, MAX_PATH);
(_tcsrchr(szFilePath, _T('\\')))[1] = 0; // 删除文件名,只获得路径字串
CString str_url = szFilePath;  // 例如str_url==e:\program\Debug\
---------------------------------------------------------
2.获得双斜杠路径不包含文件名

TCHAR _szPath[MAX_PATH + 1]={0};
GetModuleFileName(NULL, _szPath, MAX_PATH);
(_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径 字串
CString strPath;
for (int n=0;_szPath[n];n++)
{
if (_szPath[n]!=_T('\\'))
{
strPath +=_szPath[n] ;
}
else
{
strPath += _T("\\\\");
}
}

MessageBox(strPath);//输出==e:\\program\\Debug\\


二、
1:获取应用程序自身完整路径文件名
方法1:
#include "stdlib.h"
void main()
{
cout << _pgmptr << endl;
}

方法2:
char szFullPath[MAX_PATH];
ZeroMemory(szFullPath,MAX_PAT);
::GetModuleFileName(NULL,szFullPath,MAX_PATH);
::MessageBox(NULL,szFullPath,"path",MB_ICONINFORMATION);

方法3:
TCHAR szPath[MAX_PATH] = {0};
if(!GetModuleFileName(NULL, szPath, MAX_PATH))
{ return ; }
AfxMessageBox(szPath);

2:如何获取应用程序所在目录?
这里值得注意的是很多人都用
GetCurrentDirectory(MAX_PATH, szCurrentPath);
来获取。这个方法并不好,经常出错,比如现在我有一个程序在d:\test目录下,现在运行这个程序后用GetCurrentDirectory得到的是d:\test

。接着在程序里用CFileDialog来打开一个C:\test\test.txt文件后再调用GetCurrentDirectory,那么得到的szCurrentPath就是C:\test而不是d:\test。

推荐用如下方法来得到当前程序所在目录比较安全:
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
函数来分解开始提到的_pgmptr,然后再用
void _makepath( char *path, const char *drive, const char *dir, const char *fname, const char *ext );
函数来对分解后的路径进行组合。这两个函数结合起来功能强大,使用灵活,基本上所有的有关目录和路径方面的操作都可以搞定。

转载于:http://hi.baidu.com/wyuanshiy/blog/item/7818a5ec6ffab422269791dc.html

 

 MSDN的用法:

[cpp] view plain copy
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3.   
  4. int main( void )  
  5. {  
  6.    char path_buffer[_MAX_PATH];  
  7.    char drive[_MAX_DRIVE];  
  8.    char dir[_MAX_DIR];  
  9.    char fname[_MAX_FNAME];  
  10.    char ext[_MAX_EXT];  
  11.    errno_t err;  
  12.   
  13.    err = _makepath_s( path_buffer, _MAX_PATH, "c""\\sample\\crt\\",  
  14.                       "crt_makepath_s""c" );  
  15.    if (err != 0)  
  16.    {  
  17.       printf("Error creating path. Error code %d.\n", err);  
  18.       exit(1);  
  19.    }  
  20.    printf( "Path created with _makepath_s: %s\n\n", path_buffer );  
  21.    err = _splitpath_s( path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname,  
  22.                        _MAX_FNAME, ext, _MAX_EXT );  
  23.    if (err != 0)  
  24.    {  
  25.       printf("Error splitting the path. Error code %d.\n", err);  
  26.       exit(1);  
  27.    }  
  28.    printf( "Path extracted with _splitpath_s:\n" );  
  29.    printf( "  Drive: %s\n", drive );  
  30.    printf( "  Dir: %s\n", dir );  
  31.    printf( "  Filename: %s\n", fname );  
  32.    printf( "  Ext: %s\n", ext );  
  33. }  

 

我自己写了个合成当前EXE所在目录某个文件的完整路径函数:

[cpp] view plain copy
  1. void make_full_path(char* s, int nLen, const char *file_name, const char*file_ext)  
  2. {  
  3.     char szPath[MAX_PATH]={0};  
  4.     GetModuleFileNameA(NULL, szPath, MAX_PATH);  
  5.     char cDir[100] = "";  
  6.     char cDrive[10] = "";  
  7.     char cf[20] = "";  
  8.     char cExt[10] = "";  
  9.     _splitpath_s(szPath, cDrive, cDir, cf, cExt);  
  10.     _makepath_s(s, nLen, cDrive, cDir, file_name, file_ext);  
  11. }  

 

[cpp] view plain copy
  1. string GetExePath(void)  
  2. {  
  3.     char szFilePath[MAX_PATH + 1]={0};  
  4.     GetModuleFileNameA(NULL, szFilePath, MAX_PATH);  
  5.     (strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串  
  6.     string path = szFilePath;  
  7.   
  8.     return path;  
  9. }  

 

参数说明:

s用来接收完整路径;

nLen缓冲区长度;

file_name为文件名称,不带后缀;

file_ext为文件后缀。

 

[cpp] view plain copy
  1. FILE *f;  
  2. TCHAR szFilePath[MAX_PATH + 1]={0};  
  3. sprintf_s(szFilePath, "%s", g_file_in.c_str());  
  4. //GetModuleFileName(NULL, szFilePath, MAX_PATH);  
  5. (strrchr(szFilePath, '.'))[1] = 0;   
  6. sprintf_s(szFilePath, "%soutput.txt", szFilePath);  
  7.   
  8. fopen_s(&f, szFilePath, "a+");  
  9.   
  10. fwrite(strLog.c_str(), 1, strlen(strLog.c_str()), f);  
  11.   
  12. fclose(f);  
原创粉丝点击