获取工作目录

文件操作 / 基本操作

#include <unistd.h>

//getcwd 属于 POSIX(Windows 上用 direct.h 中的 _getcwd)
char workPath[FILENAME_MAX];
if (getcwd(workPath, sizeof workPath) == nullptr) {
    perror("getcwd");
    exit(EXIT_FAILURE);
}
//workPath 在沙盒中是 "/tmp/work",
//在本地机器上则是程序启动时所在的文件夹

//相对路径(如 "file.txt")从该文件夹开始;
//chdir 会为整个程序修改它

printf("workPath is %s\n", workPath);