支持 windows 和 Linux,获取程序当前路径。
github源码路径:https://github.com/dangwei-90/GetCurrentPath
// 头文件
#ifdef _WIN32
#include <io.h>
#include <tchar.h>
#include <direct.h>
#include <Windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#include <stdio.h>
// 获取当前进程的路径
std::string GetCurrentPath() {
char curr_path[1024];
#ifdef _WIN32
::GetModuleFileName(NULL, curr_path, MAX_PATH);
(_tcsrchr(curr_path, '\\'))[1] = 0;
#else
getcwd(curr_path, 1024);
sprintf(curr_path, "%s/", curr_path);
#endif
std::string str_curr_path = curr_path;
return str_curr_path;
}
// 调用
int main()
{
std::cout << GetCurrentPath() << std::endl;
}