使用 getcwd 函数获取当前目录的方法
使用 std::filesystem::current_path 函数获取当前目录
使用 get_current_dir_name 函数获取当前目录
本文将介绍几种在 C++ 中获取当前目录的方法。
使用 getcwd 函数获取当前目录的方法
getcwd 是一个兼容 POSIX 的函数,在许多基于 Unix 的系统上都可以使用,它可以检索当前的工作目录。该函数需要两个参数-第一个 char*缓冲区,其中存放目录路径名和缓冲区的大小。请注意,我们声明 char 为固定长度的数组,并分配 256 元素的缓冲区,作为一个随机的例子。如果调用成功,可以通过访问 char 缓冲区打印当前目录的存储名称。
#include
#include
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::filesystem::current_path;
int main() {
char tmp[256];
getcwd(tmp, 256);
cout
|