您的位置:首页 > 编程语言 > C语言/C++

C/C++获取当前路径

2017-03-02 16:07 281 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ c 获取路径 CString VS