c++11 获取当前可执行程序的路径

//获取当前可执行程序的路径
std::string getcurrentDir()
{
    std::experimental::filesystem::path exec_path =
            std::experimental::filesystem::absolute(
                std::experimental::filesystem::v1::current_path());
    
    return exec_path.string();;

}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取当前执行程序的创建时间,你可以使用C++的标准库函数 `std::filesystem::last_write_time`。请确保你的编译器支持C++17或更高版本,因为这个函数是在C++17中引入的。 下面是一个示例代码,演示如何获取当前执行程序的创建时间: ```cpp #include <iostream> #include <filesystem> #include <chrono> int main() { std::filesystem::path currentExecutablePath = std::filesystem::current_path() / std::filesystem::path(argv[0]); std::filesystem::file_time_type createTime = std::filesystem::last_write_time(currentExecutablePath); std::chrono::system_clock::time_point createTimePoint = std::chrono::time_point_cast<std::chrono::system_clock::duration>(createTime - decltype(createTime)::clock::now() + std::chrono::system_clock::now()); std::time_t createTimeT = std::chrono::system_clock::to_time_t(createTimePoint); std::cout << "Creation Time: " << std::ctime(&createTimeT); return 0; } ``` 在这个示例中,我们首先获取当前执行程序的路径,然后使用 `std::filesystem::last_write_time` 函数获取创建时间。由于 `last_write_time` 返回的类型是 `std::filesystem::file_time_type`,我们需要将其转换为 `std::chrono::system_clock::time_point` 类型,以便能够使用标准库的时间处理函数。 最后,我们将创建时间转换为 `std::time_t` 类型,并使用 `std::ctime` 函数将其格式化为可读的时间字符串输出。 请注意,这个方法获取的是程序执行文件的创建时间,而不是程序运行的开始时间。如果你需要获取程序运行的开始时间,可以在程序启动时使用 `std::chrono::system_clock::now()` 函数获取当前时间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值