chdir改变当前目录
来源:互联网 发布:md5用php可以解密吗 编辑:程序博客网 时间:2024/05/17 07:41
1、在实际应用中,代码需要从当前目录进到其它目录,完成操作,然后再回到当前目录。这个时候需要getcwd获取当前目录路径,保存起来,在使用chdir跳转到其它目录,然后再使用chdir和保存的路径回到最初的目录。
2、man chdir
3、int chdir(const char *path);
-参数*path;文件路径
-返回值;成功返回0,错误返回-1.
4、例:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>//chdir和fchdir函数头文件#include <unistd.h>#define LENTH 255int main(int argc,char *argv[]){int ret;char pwd[LENTH];//检测参数if(argc <3){printf("\nPlease input file path\n");return 1;}//getcwd函数获取当前目录if(!getcwd(pwd,LENTH)){perror("getcwd");return 1;}printf("\ngetcwd pwd is %s\n",pwd);//使用chdir函数转入其他目录ret = chdir(argv[1]);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",argv[1]);//转入其他目录,完成操作//使用rmdir函数删除目录ret = rmdir(argv[2]);if(ret<0){printf("rmdir %s failed!\n",argv[2]);return 1;}printf("rmdir %s is success!\n",argv[2]);//再次使用chdir回到pwd保存的目录ret = chdir(pwd);if(ret){printf("Please make sure file path\n");return 1;}printf("chdir %s is success!\n",pwd);return 0;}
0 0
- chdir改变当前目录
- chdir改变当前工作目录
- linux --chdir() 改变当前工作目录函数
- chdir()改变当前工作目录 -- Linux
- C语言chdir()函数:改变当前的工作目录
- 4.22 chdir、fchdir和getcwd函数-当前工作目录
- Python 改变当前工作目录
- Python 改变当前工作目录
- C for linux 更改当前工作目录函数 chdir 或 fchdir
- C for linux 更改当前工作目录函数 chdir 或 fchdir
- 为什么linuxC程序设计中不能使用system("cd ..")来改变当前工作路径而用chdir()函数
- OpenFileDialog改变当前目录的问题
- CD——改变当前目录
- python改变shell当前工作目录
- CFileDialog自动改变当前目录问题解决方案
- CFileDialog自动改变当前目录问题解决方案
- linux shell 改变当前工作目录
- Python 改变和获取当前工作目录
- linux命令&树莓派
- 研究生课程 算法分析-分治法
- Scrap学习笔记 --- python实现抓取图片
- tab切换fragment时,会出现滑动viewpager卡顿迟缓问题
- C语言-简单创建线程之pthread.h
- chdir改变当前目录
- 百分比布局
- java 异常com/google/protobuf/UnknownFieldSet
- JAVA byte最大值是127,最小值确实-128
- Python基础
- 单元总结(1)
- VUWE
- JavaScript - 模拟键盘输入支付密码
- const 的一些用法