[apue]函数getcwd示例疑问

发布于 2022-09-06 06:45:34 字数 789 浏览 13 评论 0

apue第三版中文版4.23节
先是一个chdir的例子讲了执行该程序后无法更改当前目录:

#include "apue.h"

int
main(void)
{
    if (chdir("/tmp") < 0)
        err_sys("chdir failed");
    printf("chdir to /tmp succeeded\n");
    exit(0);
}

然后先显示pwd 接着执行./a.out 然后pwd发现当前目录没有切换到/tmp
接着讲到getcwd函数,例子:

#include "apue.h"

int
main(void)
{
        char    *ptr;
        size_t          size;

        if (chdir("/usr/lib") < 0)
                err_sys("chdir failed");

        ptr = path_alloc(&size);        /* our own function */
        if (getcwd(ptr, size) == NULL)
                err_sys("getcwd failed");

        printf("cwd = %s\n", ptr);
        exit(0);
}

由于我的ubuntu没有/usr/spool这个目录,所以改成了/usr/lib
getcwd只是获取了当前目录,但执行完之后pwd显示还是之前的目录?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2022-09-13 06:45:34

chdir() changes the current working directory of the calling process to the directory specified in path.

man手册中说的很清楚了,chdir改变的是调用进程的当前工作目录,而不是你终端的工作目录。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文