Windows下:
string strPath;
char chpath[MAX_PATH];
GetModuleFileName(NULL, chpath, sizeof(chpath));
strPath = (string)chpath;
int pos = strPath.find_last_of('\\', strPath.length());
strPath.substr(0, pos);
std::cout<<strPath.substr(0, pos)<<std::endl;
//打印出:E:/C++/cppTest/cppPrimerTest/Debug/
以下内容参考自--- 点击
#include <windows.h>
char chpath[MAX_PATH];
GetModuleFileName(NULL,(LPSTR)chpath,sizeof(chpath));
std::cout<<chpath<<std::endl;
//打印出:E:/C++/cppTest/cppPrimerTest/Debug/cppTest.exe
#include <direct.h>
#include <stdio.h>
char buffer[MAX_PATH];
getcwd(buffer, MAX_PATH);
printf( "The current directory is: %s ", buffer);
//打印出E:/C++/cppTest/cppPrimerTest
打印出全路径:
CString path;
GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
path.ReleaseBuffer();
只提取目录:
CString path;
GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
path.ReleaseBuffer();
int pos = path.ReverseFind('\\');
path = path.Left(pos);
只提取exe名:
CString path;
GetModuleFileName(NULL,path.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
path.ReleaseBuffer();
int pos = path.ReverseFind('\\');
int len = path.GetLength();
path = path.Right(len - pos -1);