C++ 获取当前目录的跨平台方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2868680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a cross-platform way to get the current directory?
提问by rubenvb
I need a cross-platform way to get the current working directory (yes, getcwd does what I want). I thought this might do the trick:
我需要一种跨平台的方式来获取当前的工作目录(是的,getcwd 做我想要的)。我认为这可能会奏效:
#ifdef _WIN32
#include <direct.h>
#define getcwd _getcwd // stupid MSFT "deprecation" warning
#elif
#include <unistd.h>
#endif
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s_cwd(getcwd(NULL,0));
cout << "CWD is: " << s_cwd << endl;
}
I got this reading:
我得到了这样的阅读:
There should be no memory leaks, and it should work on a Mac as well, correct?
应该没有内存泄漏,它也应该在 Mac 上工作,对吗?
UPDATE: I fear something is still wrong here (I'm trying to avoid creating a char array with a determined length, as there's no proper way to get a decent length for getcwd):
更新:我担心这里仍然有问题(我试图避免创建一个具有确定长度的字符数组,因为没有正确的方法来为 getcwd 获得合适的长度):
char* a_cwd = getcwd(NULL,0);
string s_cwd(a_cwd);
free(a_cwd); // or delete a_cwd?
回答by catchmeifyoutry
If it is no problem for you to include, use boost filesystemfor convenient cross-platform filesystem operations.
如果你没有问题,可以使用boost 文件系统来方便跨平台的文件系统操作。
boost::filesystem::path full_path( boost::filesystem::current_path() );
Here is an example.
这是一个例子。
EDIT: as pointed out by Roi Danton in the comments, filesystem became part of the ISO C++ in C++17, so boost is not needed anymore:
编辑:正如 Roi Danton 在评论中指出的那样,文件系统成为C++17 中 ISO C++ 的一部分,因此不再需要 boost:
std::filesystem::current_path();
回答by R Samuel Klatchko
You cannot call getcwd
with a NULL buffer. As per the Opengroup:
您不能getcwd
使用 NULL 缓冲区进行调用。根据Opengroup:
If buf is a null pointer, the behavior of getcwd() is unspecified.
如果 buf 是空指针,则 getcwd() 的行为未指定。
Also, getcwd can return NULL which can break a string constructor.
此外, getcwd 可以返回 NULL ,这可以破坏字符串构造函数。
You'll need to change that to something like:
您需要将其更改为:
char buffer[SIZE];
char *answer = getcwd(buffer, sizeof(buffer));
string s_cwd;
if (answer)
{
s_cwd = answer;
}
回答by Matthew Flaschen
Calling getcwdwith a NULL pointer is implementation defined. It often does the allocation for you with malloc (in which case your code does have a memory leak). However, it isn't guaranteed to work at all. So you should allocate your own buffer.
使用 NULL 指针调用getcwd是实现定义的。它通常使用 malloc 为您进行分配(在这种情况下,您的代码确实存在内存泄漏)。但是,它根本不能保证工作。所以你应该分配你自己的缓冲区。
char *cwd_buffer = malloc(sizeof(char) * max_path_len);
char *cwd_result = getcwd(cwd_buffer, max_path_len);
The Open Group has an example showing how to get the max path length from _PC_PATH_MAX. You could consider using MAX_PATHon Windows. See this questionfor caveats to this number on both platforms.
Open Group 有一个示例展示了如何从 _PC_PATH_MAX 获取最大路径长度。您可以考虑在 Windows 上使用MAX_PATH。有关两个平台上此数字的警告,请参阅此问题。