20165322 第九周 实现mypwd

实现mypwd

学习pwd

  • man pwd

  • 该命令用来显示目前所在的工作目录

  • 参数

    • -P显示当前目录的物理路径
    • -L显示当前目录的连接路径
  • man -k dir | grep 2

由图可知,可以用getcwd()函数实现pwd功能

  • man -getcwd

产品代码

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define N 100

int main()
{
    char path[N];
    getcwd(path, sizeof(path));
    printf("%s 
", path);
    return 0;
}

运行截图

实验中遇到的问题

  • 第一次运行时发生段错误
  • 观察man getcwd截图发现getcwd()函数用法为char *getcwd(char *buf, size_t size);
    ,对此句代码进行修改后运行成功
原文地址:https://www.cnblogs.com/wangyaojia/p/10017569.html