阅读量:106
在C++中,可以使用以下方法获取程序运行路径:
- 使用命令行参数获取程序运行路径:
#include
int main(int argc, char* argv[]) {
if (argc > 0) {
std::cout << "Program path: " << argv class="hljs-number">0] << std class="hljs-keyword">else {
std::cout << "Unable to get program path" << std class="hljs-keyword">return 0;
}
- 使用boost库获取程序运行路径:
#include
#include
int main() {
boost::filesystem::path path = boost::filesystem::current_path();
std::cout << "Program path: " << path class="hljs-built_in">string() << std class="hljs-keyword">return 0;
}
- 使用Windows API获取程序运行路径:
#include
#include
int main() {
char path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
std::cout << "Program path: " << path class="hljs-keyword">return 0;
}
这些方法可以帮助你获取程序的运行路径,你可以根据自己的需求选择最适合的方法。