您的位置:首页 > 其它

跨平台获取当前工作目录

2010-06-01 00:16 281 查看
//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;
	}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: