chdir没起作用

chdir问题
为何使用了chdir函数之后,使用getcwd函数获取当前目录似乎是我欲改变的目录,但使用PWD查看当前工作目录并没有改变呢?代码如下:
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h> //?
#include<unistd.h>
#include<stdio.h>

#define SIZE 30


int main()
{
  char newpath[SIZE];
  char buf[SIZE];
  /*get the path of present at first*/
  if(getcwd(buf,SIZE)==NULL)
  {
  printf("error ,getcwd failed!\n");
  return -1;
  }
  printf("cwd = %s\n",buf);
  /*input the path that you want change to */
  printf("Input the new pathname[<30 strings]:\n");
  gets(newpath);
  if(chdir(newpath) == -1)//change to the directory you want
  {
  printf("error,change directory failed!\n");
  return -1;
  }
  printf("ok,change directory successful!\n");
  if(getcwd(buf,SIZE)==NULL)
  {
  printf("error ,getcwd failed!\n");
  return -1;
  }
  printf("cwd = %s\n",buf);
  return 0;
}求解中,谢谢

------解决方案--------------------
说明:chdir函数用于改变当前工作目录。调用参数是指向目录的指针,调用进程需要有搜索整个目录的权限。每个进程都具有一个当前工作目录。在解析相对目录引用时,该目录是搜索路径的开始之处。如果调用进程更改了目录,则它只对该进程有效,而不能影响调用它的那个进程。在退出程序时,shell还会返回开始时的那个工作目录。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值