如何找到当前目录?
我正在尝试读取我之前成功读取的文件。 我正在通过图书馆阅读它,并将其按原样发送到图书馆(即“myfile.txt”)。 我知道该文件是从工作/当前目录读取的。
我怀疑当前/工作目录已以某种方式更改。 我如何检查当前/工作目录是什么?
I am trying to read a file which I read previously successfully.
I am reading it through a library, and I am sending it as-is to the library (i.e. "myfile.txt").
I know that the file is read from the working/current directory.
I suspect that the current/working directory has changed somehow.
How do i check what is the current/working directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于您添加了 Visual-C++ 标签,我将建议使用标准 Windows 函数来完成此操作。 GetCurrentDirectory
用法:
Since you added the visual-c++ tag I'm going to suggest the standard windows function to do it. GetCurrentDirectory
Usage:
Boost 文件系统 库提供了一个干净的解决方案
Boost filesystem library provides a clean solution
使用
_getcwd
来获取当前工作目录。Use
_getcwd
to get the current working directory.这是我不久前得到的最与平台无关的答案:
如何从 C 的“getcwd”函数返回 std::string
它相当冗长,但确实做了它应该做的事情,有一个很好的 C++ 接口(即它返回一个字符串,而不是一个多长的字符串) -你到底是吗?-(
const
)char*
)。要关闭有关
getcwd
弃用的 MSVC 警告,您可以执行以下操作:Here's the most platform-agnostic answer I got a while ago:
How return a std::string from C's "getcwd" function
It's pretty long-winded, but does exactly what it's supposed to do, with a nice C++ interface (ie it returns a string, not a how-long-are-you-exactly?-(
const
)char*
).To shut up MSVC warnings about deprecation of
getcwd
, you can do a此代码适用于 Linux 和 Windows:
This code works for Linux and Windows:
从
C++17
开始,可以使用std::filesystem::current_path()
。https://stackoverflow.com/a/75354649/760728
Since
C++17
,std::filesystem::current_path()
can be used.https://stackoverflow.com/a/75354649/760728