(二)和菜鸟一起学习unix之文件和目录 getcwd,chdir

来源:互联网 发布:淘宝关注链接怎么获取 编辑:程序博客网 时间:2024/06/06 17:18

GETCWD(3)                  Linux Programmer’s Manual                 GETCWD(3)

NAME
       getcwd, get_current_dir_name, getwd - Get current working directory

SYNOPSIS
       #include <unistd.h>

       char *getcwd(char *buf, size_t size);

      如果buf不为空 将绝对路径存入当中  size存储 的最大空间
       char *get_current_dir_name(void);
       char *getwd(char *buf);

RETURN VALUE
       NULL on failure with errno set accordingly, and  buf  on  success.  The
       contents of the array pointed to by buf is undefined on error.
  功能:getcwd函数获得当前工作目录的绝对路径

  返回值: 成功返回指向字符串的指针,错误返回0。

例子:

  1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main(int argc,char *argv[])
  4 {
  5     char a[255];
  6     printf(getcwd(a,sizeof(a))) ;
}

CHDIR(2)                   Linux Programmer’s Manual                  CHDIR(2)

NAME
       chdir, fchdir - change working directory

SYNOPSIS
       #include <unistd.h>

       int chdir(const char *path);

        path参数你是更改到哪个目录路径名称


       int fchdir(int fd);

DESCRIPTION
       chdir()  changes  the  current  working  directory to that specified in
       path.

       fchdir() is identical to chdir();  the  only  difference  is  that  the
       directory is given as an open file descriptor.

RETURN VALUE
       On  success,  zero is returned.  On error, -1 is returned, and errno is
       set appropriately.

功能:chdir更改当前工作目录

返回值:成功返回0 错误返回-1

例子
1 #include<stdio.h>
  2 #include<unistd.h>
  3 int main(int argc,char *argv[])
  4 {
  5     char a[255];
  6     printf("\n");
  7     printf(getcwd(a,sizeof(a))) ;
  8     chdir("../");
  9     printf("\n");
10     printf(getcwd(a,sizeof(a))) ;
11
12
13
14 }
~                                                        

原创粉丝点击