跨平台获取当前工作目录

//Windows c:/ or c:/test/ //Linux / or /user/ /** * @brief getCurrentWorkingPath * * Detailed description. * @return std::string */ inline std::string getCurrentWorkingPath() { #ifdef WIN32 TCHAR workingPath[MAX_PATH]; ZeroMemory(workingPath, MAX_PATH); char path[3000]; memset(path, 0, 3000); if (GetModuleFileName(NULL, workingPath, MAX_PATH) > 0) { unsigned int index; for (unsigned int i = strlen(workingPath); i >= 0 ; --i) { if ((workingPath[i] == '//') || (workingPath[i] == ':')) { index = i; break; } } strncpy(path, workingPath, index + 1); } #else char path[3000]; memset(path, 0, 3000); getcwd(path, 3000); #endif std::string runBatWorkingPath = static_cast<std::string>(path); formatFilePath(runBatWorkingPath); return runBatWorkingPath; }

你可能感兴趣的:(跨平台获取当前工作目录)