C++中获取当前运行路径
2015-11-15 23:58
295 查看
获取.exe运行路径
多字节集环境下
Unicode 字符集环境下
多字节集环境下
#include "stdafx.h" #include <Windows.h> #include <iostream> #include <string> using namespace std; string GetProgramDir() { char exeFullPath[MAX_PATH]; // Full path string strPath = ""; GetModuleFileName(NULL,exeFullPath,MAX_PATH); strPath=(string)exeFullPath; // Get full path of the file int pos = strPath.find_last_of('\\', strPath.length()); return strPath.substr(0, pos); // Return the directory without the file name } int _tmain(int argc, _TCHAR* argv[]) { string strProgramDir = GetProgramDir(); cout<<strProgramDir<<endl; return 0; }
Unicode 字符集环境下
#include "stdafx.h" #include <Windows.h> #include <iostream> #include <string> using namespace std; string GetProgramDir() { wchar_t exeFullPath[MAX_PATH]; // Full path string strPath = ""; GetModuleFileName(NULL,exeFullPath,MAX_PATH); char CharString[MAX_PATH]; size_t convertedChars = 0; wcstombs_s(&convertedChars, CharString, MAX_PATH, exeFullPath , _TRUNCATE); strPath=(string)CharString; // Get full path of the file int pos = strPath.find_last_of('\\', strPath.length()); return strPath.substr(0, pos); // Return the directory without the file name } int _tmain(int argc, _TCHAR* argv[]) { string strProgramDir = GetProgramDir(); cout<<strProgramDir<<endl; return 0; }
相关文章推荐
- 《more effective C++》条款10 防止构造函数里的资源泄露
- 删除.cpp文件
- UML和模式应用书中C++版 NextGen POS代码
- java中数组和C语言数组的区别
- 一起talk C栗子吧(第六十回:C语言实例--字符串复制)
- C语言中可变参数的应用
- oc语言中的isa指针以及其面向对象语言的设计原理
- C++ && C 带空格的字符串输入
- c++单元测试指南:使用google test
- C语言关键字
- C语言的队列的初始化和先进先出的简洁程序
- C++ 定义类类型对象,默认构造函数,带有默认参数的构造函数
- C++学习笔记23——关联容器之set
- Dex动态加载的C语言部分
- 从初始化列表和构造函数谈C++的初始化机制
- VC++ CArchive及简单的文件操作方法
- c++(父类与子类,继承)
- C语言 scanf函数的机理和常规的格式字符串
- C++学习笔记22——关联容器之map
- C++中的friend友元函数详细解析