c/c++ 获取当前程序(EXE)所在的路径
2015-09-22 20:36
731 查看
一、
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++)
{
if (_szPath
!=_T('\\'))
{
strPath +=_szPath
;
}
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
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
errno_t err;
err = _makepath_s( path_buffer, _MAX_PATH, "c", "\\sample\\crt\\",
"crt_makepath_s", "c" );
if (err != 0)
{
printf("Error creating path. Error code %d.\n", err);
exit(1);
}
printf( "Path created with _makepath_s: %s\n\n", path_buffer );
err = _splitpath_s( path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname,
_MAX_FNAME, ext, _MAX_EXT );
if (err != 0)
{
printf("Error splitting the path. Error code %d.\n", err);
exit(1);
}
printf( "Path extracted with _splitpath_s:\n" );
printf( " Drive: %s\n", drive );
printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", fname );
printf( " Ext: %s\n", ext );
}
我自己写了个合成当前EXE所在目录某个文件的完整路径函数:
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
void make_full_path(char* s, int nLen, const char *file_name, const char*file_ext)
{
char szPath[MAX_PATH]={0};
GetModuleFileNameA(NULL, szPath, MAX_PATH);
char cDir[100] = "";
char cDrive[10] = "";
char cf[20] = "";
char cExt[10] = "";
_splitpath_s(szPath, cDrive, cDir, cf, cExt);
_makepath_s(s, nLen, cDrive, cDir, file_name, file_ext);
}
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
string GetExePath(void)
{
char szFilePath[MAX_PATH + 1]={0};
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串
string path = szFilePath;
return path;
}
参数说明:
s用来接收完整路径;
nLen缓冲区长度;
file_name为文件名称,不带后缀;
file_ext为文件后缀。
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/9e12f1d3e499fc949c886e7c9e0484f9)
FILE *f;
TCHAR szFilePath[MAX_PATH + 1]={0};
sprintf_s(szFilePath, "%s", g_file_in.c_str());
//GetModuleFileName(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '.'))[1] = 0;
sprintf_s(szFilePath, "%soutput.txt", szFilePath);
fopen_s(&f, szFilePath, "a+");
fwrite(strLog.c_str(), 1, strlen(strLog.c_str()), f);
fclose(f);
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++)
{
if (_szPath
!=_T('\\'))
{
strPath +=_szPath
;
}
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
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
errno_t err;
err = _makepath_s( path_buffer, _MAX_PATH, "c", "\\sample\\crt\\",
"crt_makepath_s", "c" );
if (err != 0)
{
printf("Error creating path. Error code %d.\n", err);
exit(1);
}
printf( "Path created with _makepath_s: %s\n\n", path_buffer );
err = _splitpath_s( path_buffer, drive, _MAX_DRIVE, dir, _MAX_DIR, fname,
_MAX_FNAME, ext, _MAX_EXT );
if (err != 0)
{
printf("Error splitting the path. Error code %d.\n", err);
exit(1);
}
printf( "Path extracted with _splitpath_s:\n" );
printf( " Drive: %s\n", drive );
printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", fname );
printf( " Ext: %s\n", ext );
}
我自己写了个合成当前EXE所在目录某个文件的完整路径函数:
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
void make_full_path(char* s, int nLen, const char *file_name, const char*file_ext)
{
char szPath[MAX_PATH]={0};
GetModuleFileNameA(NULL, szPath, MAX_PATH);
char cDir[100] = "";
char cDrive[10] = "";
char cf[20] = "";
char cExt[10] = "";
_splitpath_s(szPath, cDrive, cDir, cf, cExt);
_makepath_s(s, nLen, cDrive, cDir, file_name, file_ext);
}
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
string GetExePath(void)
{
char szFilePath[MAX_PATH + 1]={0};
GetModuleFileNameA(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '\\'))[0] = 0; // 删除文件名,只获得路径字串
string path = szFilePath;
return path;
}
参数说明:
s用来接收完整路径;
nLen缓冲区长度;
file_name为文件名称,不带后缀;
file_ext为文件后缀。
[cpp] view
plaincopy
![](https://oscdn.geek-share.com/Uploads/Images/Content/201611/a7c8e286f463007e2a900848b93dd72c.png)
FILE *f;
TCHAR szFilePath[MAX_PATH + 1]={0};
sprintf_s(szFilePath, "%s", g_file_in.c_str());
//GetModuleFileName(NULL, szFilePath, MAX_PATH);
(strrchr(szFilePath, '.'))[1] = 0;
sprintf_s(szFilePath, "%soutput.txt", szFilePath);
fopen_s(&f, szFilePath, "a+");
fwrite(strLog.c_str(), 1, strlen(strLog.c_str()), f);
fclose(f);
相关文章推荐
- opencv在C++窗体程序中 导包出错
- C++中相对路径与绝对路径以及斜杠与反斜杠的区别
- C++学习记录之string字符串
- C语言程序初体验-第六课-第三题:一周工资数
- C语言中枚举的使用
- C++多线程编程
- C语言程序初体验-第六课-第一题:两个正整数的正差值
- 【C语言】 字符串的内存拷贝处理函数
- C语言程序初体验-第六课-第二题:三个整数比较
- More Effective C++ 条款15 了解异常处理(exception handling)的成本
- eclipse 利用已有c++代码建工程,并编译执行
- 用c语言计算1/1-1/2+1/3-1/4+1/5-...+1/99-1/100
- 用c语言统计一个字符串中有多少个数字字符
- VS2013 配置全局 VC++ 目录
- C++ - 派生类访问模板基类(templatized base class)命名
- 用c语言求ax^2+bx+c=0方程的解
- C++ STL(标准模板类) 学习笔记
- C++ 如何控制浮点数的位数
- C++编写四则运算生成程序
- C++学习记录之vector容器