Change the current working directory in C++(更改 C++ 中的当前工作目录)
问题描述
如何以与平台无关的方式在 C++ 中更改我当前的工作目录?
How can I change my current working directory in C++ in a platform-agnostic way?
我找到了与 Windows 兼容的 direct.h
头文件和与 UNIX/POSIX 兼容的 unistd.h
.
I found the direct.h
header file, which is Windows compatible, and the unistd.h
, which is UNIX/POSIX compatible.
推荐答案
chdir
函数适用于 POSIX (manpage)和 Windows(称为 _chdir
那里,但别名 chdir
存在).
The chdir
function works on both POSIX (manpage) and Windows (called _chdir
there but an alias chdir
exists).
两种实现都在成功时返回零,在错误时返回 -1.正如您在手册页中看到的那样,在 POSIX 变体中可能会出现更显着的 errno 值,但对于大多数用例来说,这应该不会真正产生影响.
Both implementations return zero on success and -1 on error. As you can see in the manpage, more distinguished errno values are possible in the POSIX variant, but that shouldn't really make a difference for most use cases.
这篇关于更改 C++ 中的当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 C++ 中的当前工作目录


基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01