Chinaunix
标题:
如何在程序中调用cd命令
[打印本页]
作者:
shenzhengxuan
时间:
2006-12-04 14:52
标题:
如何在程序中调用cd命令
linux下有个system函数可以用来调用各种命令,但执行
system("cd /");
却没有正确执行。请问各位大侠如何调用cd命令来改变路径呢?非常感谢
作者:
mq110
时间:
2006-12-04 15:22
man chdir
作者:
owenyang1984
时间:
2006-12-04 15:25
什么系统
作者:
shenzhengxuan
时间:
2006-12-04 15:28
谢谢mq110
不能通过system调用来实现么?redhat9.0。
作者:
wuzhao
时间:
2006-12-04 16:07
system调用是通过fork子进程后exec实现的
作者:
mq110
时间:
2006-12-04 17:29
原帖由
shenzhengxuan
于 2006-12-4 15:28 发表
谢谢mq110
不能通过system调用来实现么?redhat9.0。
正如楼上的所说,system不能改变当前进程的current working directory,改变的只是子进程的current working directory不影响当前进程.
作者:
duanjigang
时间:
2006-12-05 13:13
要更改目录吗,为什么要用system函数,而不这样
#ifndef _WIN32
#include <unistd.h>
#endif
#include <stdio.h>
int main(void)
{
char olddir[100],newdir[100];
char *old = getcwd(olddir, 100);
char * new ;
printf("current dir is: %s\n", old);
chdir("../../");
new = getcwd(newdir, 100);
printf("change dir to : %s\n", new);
chdir(olddir);
getcwd(newdir, 100);
printf("back to: %s\n", newdir);
}
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2