获取应用开始运行的目录(C++ 代码)

get the directory where app starts to run (c++ code)

本文关键字:C++ 代码 应用 开始 运行 获取      更新时间:2023-10-16

我希望得到应用程序的当前工作路径。我知道objective-c代码。但我更喜欢 c++ 代码。

欢迎您的评论

您可以使用

getcwd函数:

#include <unistd.h>
#include <cstdio>
#include <iostream>
#include <cstdlib>
int main()
{
    char *cwd;
    if ((cwd = getcwd(NULL, 64)) == NULL) {
        perror("pwd");
        exit(2);
    }
    std::cout <<  cwd << std::endl;;
    free(cwd); /* free memory allocated by getcwd() */
    return 0;
}

此外,您可以使用 Boost.FileSystem 和 current_path() 方法:

#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
    boost::filesystem::path p = boost::filesystem::current_path();
    std::cout << p << std::endl;
    return 0;
}