迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中获取当前目录

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将介绍几种在 C++ 中获取当前目录的方法。


使用 getcwd 函数获取当前目录的方法

getcwd 是一个兼容 POSIX 的函数,在许多基于 Unix 的系统上都可以使用,它可以检索当前的工作目录。该函数需要两个参数-第一个 char*缓冲区,其中存放目录路径名和缓冲区的大小。请注意,我们声明 char 为固定长度的数组,并分配 256 元素的缓冲区,作为一个随机的例子。如果调用成功,可以通过访问 char 缓冲区打印当前目录的存储名称。

#include <unistd.h>

#include <filesystem>
#include <iostream>
#include <string>

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 << "Current working directory: " << tmp << endl;

  return EXIT_SUCCESS;
}

输出:

Current working directory: /tmp/projects

使用 std::filesystem::current_path 函数获取当前目录

std::filesystem::current_path 方法是 C++ 文件系统库的一部分,是现代编码准则推荐的检索当前目录的方式。需要注意的是,filesystem 库是在 C++17 版本之后加入的,要想使用下面的代码,需要相应版本的编译器。

std::filesystem::current_path 在没有任何参数的情况下,返回当前的工作目录,可以直接输出到控制台。如果将目录路径传递给函数,则将当前目录改为给定路径。

#include <unistd.h>

#include <filesystem>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::filesystem::current_path;

int main() {
  cout << "Current working directory: " << current_path() << endl;

  return EXIT_SUCCESS;
}

输出:

Current working directory: /tmp/projects

使用 get_current_dir_name 函数获取当前目录

get_current_dir_name 是 C 库函数,与前面的方法类似,只是在调用成功后返回 char*,存储当前工作目录的路径。get_current_dir_name 会自动分配足够的动态内存来存储路径名,但调用者有责任在程序退出前释放内存。

#include <unistd.h>

#include <filesystem>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::filesystem::current_path;

int main() {
  char *cwd = get_current_dir_name();
  cout << "Current working directory: " << cwd << endl;
  free(cwd);

  return EXIT_SUCCESS;
}

输出:

Current working directory: /tmp/projects

C++ 文件系统库的另一个有用的功能是 std::filesystem::directory_iterator 对象,它可以用来遍历给定目录路径中的元素。注意,元素可以是文件、符号链接、目录或套接字,它们的迭代顺序是不指定的。在下面的例子中,我们输出当前工作目录中每个元素的路径名。

#include <unistd.h>

#include <filesystem>
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::filesystem::current_path;
using std::filesystem::directory_iterator;

int main() {
  for (auto& p : directory_iterator("./")) cout << p.path() << '\n';

  return EXIT_SUCCESS;
}

输出:

"./main.cpp"
"./a.out"

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便