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:
我读到了这个:
_getcwd
at MSDNgetcwd
at Kernel.orggetcwd
at Apple.com
在MSDN上_getcwd
getcwd在Kernel.org
来自Apple.com的getcwd
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):
更新:我担心这里仍然存在错误(我试图避免创建一个具有确定长度的char数组,因为没有正确的方法来获得getcwd的合适长度):
char* a_cwd = getcwd(NULL,0);
string s_cwd(a_cwd);
free(a_cwd); // or delete a_cwd?
3 个解决方案
#1
13
You cannot call getcwd
with a NULL buffer. As per the Opengroup:
您不能使用NULL缓冲区调用getcwd。根据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;
}
#2
25
If it is no problem for you to include, use boost filesystem for 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();
#3
2
Calling getcwd with 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_PATH on Windows. See this question for caveats to this number on both platforms.
Open Group有一个示例显示如何从_PC_PATH_MAX获取最大路径长度。您可以考虑在Windows上使用MAX_PATH。在两个平台上都可以看到这个问题。
#1
13
You cannot call getcwd
with a NULL buffer. As per the Opengroup:
您不能使用NULL缓冲区调用getcwd。根据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;
}
#2
25
If it is no problem for you to include, use boost filesystem for 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();
#3
2
Calling getcwd with 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_PATH on Windows. See this question for caveats to this number on both platforms.
Open Group有一个示例显示如何从_PC_PATH_MAX获取最大路径长度。您可以考虑在Windows上使用MAX_PATH。在两个平台上都可以看到这个问题。