Chinaunix首页 | 论坛 | 博客
  • 博客访问: 76491
  • 博文数量: 18
  • 博客积分: 1530
  • 博客等级: 上尉
  • 技术积分: 196
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-18 17:51
文章分类

全部博文(18)

文章存档

2011年(4)

2010年(14)

我的朋友

分类: LINUX

2010-12-14 12:28:32

这三个函数要配合使用!
 
chdir(改变当前的工作目录)
相关函数 getcwd,chroot
表头文件 #include
定义函数 int chdir(const char * path);
函数说明 chdir()用来将当前的工作目录改变成以参数path所指的目录。
返回值 执行成功则返回0,失败返回-1,errno为错误代码。
范例
#include
main()
{
 chdir(“/tmp”);
 printf(“current working directory: %s ”,getcwd(NULL,NULL));
}
执行
current working directory :/tmp
 
fchdir(改变当前的工作目录)
相关函数 getcwd,chroot
表头文件 #include
定义函数 int fchdir(int fd);
函数说明 fchdir()用来将当前的工作目录改变成以参数fd所指的文件描述
词。
返回值 执行成功则返回0,失败返回-1,errno为错误代码。
附加说明
范例
#include
#include
#include
#include
main()
{
  int fd;
  fd = open(“/tmp”,O_RDONLY);
  fchdir(fd);
  printf(“current working directory : %s ”,getcwd(NULL,NULL));
  close(fd);
}
执行
current working directory : /tmp
 
 
getcwd(取得当前的工作目录)
相关函数 get_current_dir_name,getwd,chdir
表头文件 #include
定义函数 char * getcwd(char * buf,size_t size);
函数说明 getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内
存空间,参数size为buf的空间大小。在调用此函数时,buf所指的
内存空间要足够大,若工作目录绝对路径的字符串长度超过参数
size大小,则回值NULL,errno的值则为ERANGE。倘若参数buf
为 NULL,getcwd()会依参数 size 的大小自动配置内存(使用
malloc()),如果参数size也为0,则getcwd()会依工作目录
绝对路径的字符串程度来决定所配置的内存大小,进程可以在使用
完此字符串后利用free()来释放此空间。
返回值 执行成功则将结果复制到参数buf所指的内存空间,或是返回自动
配置的字符串指针。失败返回NULL,错误代码存于errno。
范例
#include
main()
{
  char buf[80];
  getcwd(buf,sizeof(buf));
  printf(“current working directory : %s ”,buf);
}
执行
current working directory :/tmp
 
 
阅读(2929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~