我知道如何通过getcwd
函数获取CWD 的路径名,我正在使用strtok
函数来获取目录名(表示当前工作文件夹),有没有什么可以通过简单的查询或函数来获取这个东西?
最佳答案
如果 getcwd()
返回 "/this/is/my/cwd"
而你只想要 "cwd"
那么你可能喜欢使用 basename()
关于 getcwd()
返回的内容。
#include <unistd.h> /* for getcwd() */
#include <libgen.h> /* for basename() */
[...]
char cwd[PATH_MAX] = "";
char * cwd_base = NULL;
if (NULL == getcwd(cwd, sizeof(cwd)))
{
perror("getcwd() failed");
}
else
{
cwd_base = basename(cwd);
}
if (NULL != cwd_base)
{
printf("The current working directory's base name is '%s'.\n", cwd_base);
}
关于c++ - 如何获取当前工作目录(current working folder)名称而不是路径名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22452314/