C++17的filesystem提供了current_path可以获得启动程序的目录:
#include <iostream>
#include <filesystem>
#include <string>
using namespace std;
namespace fs = std::filesystem;
int main()
{
string path = fs::current_path().string();
cout << "path:"<<path<<endl;
return 0;
}
需要说明的是:
这个目录是启动程序的目录,假设test程序位于/tmp/aaa/test;如果在目录/tmp/aaa中启动的test,那么程序输出/tmp/aaa,但如果在目录/tmp中通过./aaa/test的方式启动程序,那么输出的目录是/tmp。